线程的终止pthread_exit和返回为什么终止的原因

#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
");
}

static void write_wifi_messgage(void)
{
    printf("write wifi messgage
");
}
static  void  critic_data(void)
{
     read_wifi_message();
     if(1)
     {
       write_wifi_messgage();
     }
     printf("current sensor gain
");
     printf("vedio current signal
");
}

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
");
      return 0;
   }
   printf("I'm here %s
",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
",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
",ret,(int)thread_res[0]);

    while(1);

    return 0;
}

  

一勤天下无难事。
原文地址:https://www.cnblogs.com/nowroot/p/13643046.html