執行緒的終止pthread_exit和返回為什麼終止的原因
阿新 • • 發佈:2020-11-17
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <signal.h> static int pthread_run = 1; static void print(char*s); static pthread_spinlock_t spin_lock; static void read_wifi_message(void) { printf("read_wifi_message\n"); } static void write_wifi_messgage(void) { printf("write wifi messgage\n"); } static void critic_data(void) { read_wifi_message(); if(1) { write_wifi_messgage(); } printf("current sensor gain\n"); printf("vedio current signal\n"); } static void* Z_M_Pthread(void *arg) { int a ; while(pthread_run) { pthread_spin_lock(&spin_lock); critic_data(); pthread_spin_unlock(&spin_lock); pthread_run = 4; // 網路太差 pthread_exit(&pthread_run); } return (void*)NULL; } // 定義一個臨界區資源 static void print(char*s) { if(s==NULL) { printf("int value error\n"); return 0; } printf("I'm here %s\n",s); } int main() { int ret = -1; int* thread_res = NULL; pthread_t z_mtid,m_stid; //pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE); pthread_spin_init(&spin_lock,PTHREAD_PROCESS_PRIVATE); printf("ret is %d\n",ret); ret = pthread_create(&z_mtid,NULL,Z_M_Pthread,NULL); ret = pthread_join(z_mtid,&thread_res); printf("ret is %d pthread_exit is %d\n",ret,(int)thread_res[0]); while(1); return 0; }