linux下线程间通过条件变量同步以及函数退出

#include <stdio.h>#include <pthread.h>#include "stdlib.h"#include "unistd.h&quo
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"

pthread_mutex_t mutex;
pthread_cond_t cond;

void Unlock(void * in)
{
    (void)pthread_mutex_unlock(&mutex);
}

void *thread1(void *in)
{
    pthread_cleanup_push(Unlock, &mutex);
    /*PTHREAD_CANCEL_DISABLE 线程不可取消,
    收到的取消请求将挂起,直到将线程取消状态置为启用。
    PTHREAD_CANCEL_ENABLE 线程可以取消(默认)*/
    /*如果线程设为可以取消,那么可以通过
    int pthread_setcanceltype(int type, int *oldtype)设置类型
    type值可以为如下:
    PTHREAD_CANCEL_ASYNCHRONOUS 可能会在任何点取消线程。异步取消应用场景很少。
    PTHREAD_CANCEL_DEFERRED 延迟取消(取消请求挂起),直至到达取消点。*/
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE , NULL);
    while (true) 
    {
        printf("thread1 is running\n");
        //调用pthread_cond_wait之前一定要加锁,之后一定解锁
        pthread_mutex_lock(&mutex);
        /*这个函数在线程挂起进入等待前将mutex解锁。
        在条件满足(备唤醒)从而离开pthread_cond_wait()之前,mutex将被重新加锁*/
        pthread_cond_wait(&cond,&mutex);//这个函数也是取消点
        printf("thread1 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        //pthread_testcancel();
        sleep(2);//sleep也是取消点
    }
    /*与pthread_cleanup_push(Unlock, &mutex)成对出现,**一定要在同级下**,
    当线程异常退出(包括走到取消点退出时,会调用hander函数释放资源)*/
    pthread_cleanup_pop(0);
}

void *thread2(void *in)
{
    pthread_cleanup_push(Unlock, &mutex);
    pthread_setcancelstate(NULL, NULL);//默认PTHREAD_CANCEL_ENABLE
    while (true) 
    {
        printf("thread2 is running\n");
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread2 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        //pthread_testcancel();
        //sleep(2);
    }
    pthread_cleanup_pop(0);
}

void *thread3(void *in)
{
    pthread_cleanup_push(Unlock, &mutex);
    while (true) 
    {
        printf("thread3 is running\n");
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread3 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        //pthread_testcancel();
        //sleep(2);
    }
    pthread_cleanup_pop(0);
}

int main()
{
    printf("start\n");
    pthread_t thid1,thid2,thid3;
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_create(&thid1,NULL,thread1,NULL);
    pthread_create(&thid3,NULL,thread3,NULL);
    pthread_create(&thid2,NULL,thread2,NULL);
    sleep(1);
    for (int i=0;i<6;i++)
    {
        sleep(3);
        printf("times :%d \n", i);
        if (i == 2)
        {
            pthread_cancel(thid2);
            pthread_cancel(thid1);
        }
        pthread_cond_signal(&cond);
        
    };
    sleep(2);
    //pthread_exit(0);
    /*pthread_join()函数会一直阻塞调用线程,直到指定的线程终止。当pthread_join()
    返回之后,应用程序可回收与已终止线程关联的任何数据存储空间。 但是,同时需要注意,
    一定要和上面创建的某一线程配套使用,这样还可以起到互斥的作用。否则多线程可能抢占
    CPU资源,导致运行结果不确定。因此这里thid3 join无效,线程一直在运行并没有终止*/
    pthread_join(thid1,NULL);
    //pthread_join(thid3,NULL);
    pthread_join(thid2,NULL);
    printf("return\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值