首页 > 编程语言 > 详细

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

时间:2020-09-10 00:29:01      阅读:131      评论:0      收藏:0      [点我收藏+]
#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;
}

  技术分享图片

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

原文:https://www.cnblogs.com/nowroot/p/13643046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!