linux条件变量相关测试

本文探讨了多线程编程中线程管理与同步机制的应用,包括线程取消状态设置、条件变量使用方法及线程间如何通过互斥锁和条件变量进行同步。通过实例演示了不同线程在运行过程中如何等待条件变化并响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关参考注释学习

#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"
#include <sys/time.h>
#include <time.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_pop(0);//与pthread_cleanup_push(Unlock, &mutex)成对出现,一定要在同级下,当线程异常退出(包括走到取消点退出时,会调用hander函数释放资源)
}

void *thread2(void *in)
{
    pthread_cleanup_push(Unlock, &mutex);
    pthread_setcancelstate(NULL , NULL);//默认PTHREAD_CANCEL_ENABLE
    int i=-1;
    while (true) 
    {
        struct timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts);
        //struct timeval now;
        //struct timespec outtime;
        //gettimeofday(&now, NULL);
        //outtime.tv_sec = now.tv_sec + 10000;
        //outtime.tv_nsec = now.tv_usec * 1000;
        ts.tv_sec = ts.tv_sec + 1;//这里不能传入小数,试了好久
        printf("thread2 is running\n");
        pthread_mutex_lock(&mutex);
        //pthread_cond_wait(&cond,&mutex);
        //pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t *mutex, const struct timespec * abstime)
        //pthread_cond_timedwait 如果超时,则返回;如果等待到条件变量cond,也返回。
        //第三个参数是绝对时间
        pthread_cond_timedwait(&cond, &mutex, &ts);
        printf("%d thread2 applied the condition\n", ++i);
        pthread_mutex_unlock(&mutex);
        //pthread_testcancel();
        //sleep(2);
    }
    pthread_cleanup_pop(0);
}

void *thread3(void *in)
{
    pthread_cleanup_push(Unlock, &mutex);
    pthread_setcancelstate(NULL , NULL);//默认PTHREAD_CANCEL_ENABLE
    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<5;i++)
    {
        printf("times :%d \n", i);
        pthread_mutex_lock(&mutex);
        //pthread_cond_signal(&cond);//唤醒N多个等待中的一个,具体哪个被唤醒,依赖于操作系统
        pthread_cond_broadcast(&cond);//唤醒所有的等待
        pthread_mutex_unlock(&mutex);
        sleep(2);
        
    };
    //sleep(2);
    pthread_cancel(thid2);//使线程退出
    pthread_cancel(thid1);
    pthread_cancel(thid3);
    //pthread_exit(0);
    //pthread_join()函数会一直阻塞调用线程,直到指定的线程终止。当pthread_join()/返回之后,应用程序可回收与已终止线程关联的任何数据存储空间。 
    pthread_join(thid1,NULL);
    pthread_join(thid3,NULL);
    pthread_join(thid2,NULL);
    printf("return\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值