Liunx系统编程篇—多线程编程(三)(关于条件变量)常用API的使用

条件变量

条件变量是的同步机制。

条件变量给多个线程提供了一个会合的场所。条件变量与互斥量一起使用时,允许线程以无竞争的方式等待特定的条件发生。
条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。

条件变量使用之前必须首先初始化

pthread_cond_t数据类型代表的条件变量可以用两种方式进行初始化,

静态初始化:

pthread_cond_t cond = PTHREAD_COND_INITIALIZER可以把常量PTHREAD_COND_INITIALIZER赋给静态分配的条件变量。

动态初始化:

pthread_cond_init(&cond,NULL);如果条件变量是动态分配的,可以使用pthread_cond_destroy函数对条件变量进行去除初始化(deinitialize)。

常用API

包含头文件

#include <pthread.h>

1. 创建及销毁条件变量

函数原型

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);

函数参数

*cond:全局变量pthread_cond_t cond的地址pthread_cond_destroy(&cond);
attr:除非需要创建一个非默认属性的条件变量,否则pthread_cont_init函数的attr参数可以设置为NULL。pthread_cond_init(&cond,NULL);

返回值

返回:若成功返回0,否则返回错误编号

2. 等待

pthread_cond_wait等待条件变为真。如果在给定的时间内条件不能满足,那么会生成一个代表一个出错码的返回变量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线程列表上,然后对互斥量解锁,这两个操作都是原子操作。这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道,这样线程就不会错过条件的任何变化。pthread_cond_wait返回时,互斥量再次被锁住。
pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout。timeout指定了等待的时间,它是通过timespec结构指定。

函数原型

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);

函数参数

*cond:全局变量pthread_cond_t cond的地址。
*restrict mutex:全局变量pthread_mutex_t mutex的地址。

返回值

返回:若成功返回0,否则返回错误编号

3. 触发

这两个函数可以用于通知线程条件已经满足。pthread_cond_signal函数将唤醒等待该条件的某个线程,而pthread_cond_broadcast函数将唤醒等待该条件的所有进程。

函数原型

int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);

函数参数

*cond:全局变量pthread_cond_t cond的地址。

返回值

返回:若成功返回0,否则返回错误编号

实例:

线程条件控制实现线程的同步
#include <stdio.h>
#include <pthread.h>
//int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int a=0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
    
    
        while(1){
    
    
                pthread_cond_wait(&cond,&mutex);
                printf("=============\n");
                //printf("t1:a=%d\n",a);
                a=0;
                sleep(1);
        }
}
void *func2(void *arg)
{
    
    
        while(1){
    
    
                printf("t2:a=%d\n",a);
                pthread_mutex_lock(&mutex);
                a++;
                if(a == 3){
    
    
                        pthread_cond_signal(&cond);
                }
                pthread_mutex_unlock(&mutex);
                sleep(1);
        }
}
int main()
{
    
    
        int ret1=0;
        int ret2=0;
        pthread_t t1;
        pthread_t t2;
        pthread_mutex_init(&mutex, NULL);
        ret1=pthread_create(&t1,NULL,func1,NULL);
        ret2=pthread_create(&t2,NULL,func2,NULL);
        pthread_cond_init(&cond,NULL);
        if((ret1||ret2)!=0){
    
    
                printf("main:create thread failure\n");
        }
        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        return 0;
}

实验结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44933419/article/details/113485276