Linux线程同步——条件变量

互斥锁是用来给资源上锁的,而条件变量是用来等待而不是用来上锁的。

条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。

通常条件变量和互斥锁同时使用。

和条件变量使用有关的几个重要函数:

int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);     
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
int pthread_cond_destroy(pthread_cond_t *cond);  
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);  //解除所有线程的阻塞

1. 初始化:

    条件变量采用的数据类型是pthread_cond_t, 在使用之前必须要进行初始化, 这包括两种方式:

  • 静态: 可以把常量PTHREAD_COND_INITIALIZER给静态分配的条件变量.
  • 动态: pthread_cond_init函数, 是释放动态条件变量的内存空间之前, 要用pthread_cond_destroy对其进行清理.

#include <pthread.h>

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

成功则返回0, 出错则返回错误编号.

    当pthread_cond_init的attr参数为NULL时, 会创建一个默认属性的条件变量; 非默认情况以后讨论.

2. 等待条件:

#include <pthread.h>

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

成功则返回0, 出错则返回错误编号.

    这两个函数分别是阻塞等待和超时等待.

    等待条件函数等待条件变为真, 传递给pthread_cond_wait的互斥量对条件进行保护, 调用者把锁住的互斥量传递给函数. 函数把调用线程放到等待条件的线程列表上, 然后对互斥量解锁, 这两个操作是原子的. 这样便关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道, 这样线程就不会错过条件的任何变化.

    当pthread_cond_wait返回时, 互斥量再次被锁住.

3. 通知条件:

#include <pthread.h>

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

成功则返回0, 出错则返回错误编号.

    这两个函数用于通知线程条件已经满足. 调用这两个函数, 也称向线程或条件发送信号. 必须注意, 一定要在改变条件状态以后再给线程发送信号.

条件变量使用示例

下面是个非常简单的例子,很好的演示了条件变量如何使用。

我将创建一个生产者线程,两个消费者线程,通过互斥量和条件变量同步他们的生产和消费操作。

需要注意的一个地方是,pthread_cond_wait操作必须传入一个条件变量和互斥量,线程先对互斥量上锁,然后再执行pthread_cond_wait,pthread_cond_wait会将互斥量解锁。

pthread_cond_wait在while循环中而不在if中的原因是,“可能被意外唤醒”,所以需要在唤醒之后再检查缓冲区。

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <unistd.h>
 4 
 5 pthread_cond_t condc,condp;
 6 pthread_mutex_t the_mutex;
 7 
 8 unsigned int buffer = 0;
 9 const int MAX = 100;
10 
11 void *producer(void *ptr){
12     for(int i = 1; i < MAX; i++){
13         pthread_mutex_lock(&the_mutex);
14         while(buffer != 0) pthread_cond_wait(&condp, &the_mutex);
15         sleep(1);
16         buffer = i;
17         printf("producer pthread produce one production %d.\n", i);
18         pthread_cond_broadcast(&condc);//唤醒两个消费者线程
19         pthread_mutex_unlock(&the_mutex);
20     }
21     pthread_exit(0);
22 }
23 
24 void *consumer1(void *ptr){
25     for(int i = 1; i < MAX; i++){
26         pthread_mutex_lock(&the_mutex);
27         while(buffer == 0) pthread_cond_wait(&condc, &the_mutex);    
28         printf("consumer1 pthread consume one production %d.\n", buffer);
29         buffer = 0;
30         pthread_cond_signal(&condp);
31         pthread_mutex_unlock(&the_mutex);
32     }
33     pthread_exit(0);
34 }
35 void *consumer2(void *ptr){
36     for(int i = 1; i < MAX; i++){
37         pthread_mutex_lock(&the_mutex);
38         while(buffer == 0) pthread_cond_wait(&condc, &the_mutex);    
39         printf("consumer2 pthread consume one production %d.\n", buffer);
40         buffer = 0;
41         pthread_cond_signal(&condp);
42         pthread_mutex_unlock(&the_mutex);
43     }
44     pthread_exit(0);
45 }
46 
47 int main(void){
48     pthread_t pro, con1, con2;
49     pthread_mutex_init(&the_mutex,0);
50     pthread_cond_init(&condc,0);
51     pthread_cond_init(&condp,0);
52     pthread_create(&con1, 0, consumer1, 0);
53     pthread_create(&pro, 0, producer, 0);
54     pthread_create(&con2, 0, consumer2, 0);
55     pthread_join(pro, 0);
56     pthread_join(con1, 0);
57     pthread_join(con2, 0);
58     pthread_cond_destroy(&condc);
59     pthread_cond_destroy(&condp);
60     pthread_mutex_destroy(&the_mutex);
61     return 0;
62 }

运行结果

猜你喜欢

转载自www.cnblogs.com/liangf27/p/9493722.html