pthread条件变量
muduo/base/Condition.h中有对条件变量的封装,底层是pthread 条件变量。
条件变量
条件变量是线程使用的一种同步机制。条件变量给多个线程提供了会合的场所。条件变量和互斥量一起用的时候,允许线程以无竞争的方式等待特定的条件发生。条件是受互斥量保护的。线程在改变条件状态时必须首先对互斥量加锁。
相关函数
1.初始化与销毁
pthread_cond_t数据类型表示条件变量,使用以下两个函数对条件变量进行初始化和销毁:
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
注:关键字restrict只用于限定指针;该关键字用于告知编译器,所有修改该指针所指向内容的操作全部都是基于(base on)该指针的,即不存在其它进行修改操作的途径;这样的后果是帮助编译器进行更好的代码优化,生成更有效率的汇编代码。
2.等待条件变量
我们使用pthread_cond_wait等待条件变量变为真:
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
当线程调用pthread_cond_wait()函数时,会将调用线程放到等待条件的线程列表上,并原子的对互斥量解锁(这样就不会死锁)。当pthread_cond_wait()返回时,互斥量再次锁住。
3.通知线程条件满足
我们使用两个函数可以通知线程条件已经满足。pthread_cond_signal函数至少能唤醒一个等待该条件的线程。pthread_cond_broadcast函数能唤醒等待该条件的所有线程。
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t * cond);
int pthread_cond_broadcast(pthread_cond_t * cond);
这两个函数都可以给条件等待线程发信号,不过需要注意的是,一定要在改变条件状态以后再给线程发信号。示例
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 3
#define TCOUNT 10
#define COUNT_LIMIT 12
int count = 0;
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;
void *inc_count(void *t) //增加count
{
int i;
long my_id = (long)t;
for (i=0; i < TCOUNT; i++)
{
pthread_mutex_lock(&count_mutex);
count++;
if (count == COUNT_LIMIT) //满足条件后
{
printf("inc_count(): thread %ld, count = %d Threshold reached. ",my_id, count);
pthread_cond_signal(&count_threshold_cv);//通知
printf("Just sent signal.\n");
}
printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",my_id, count);
//这里释放锁的同时 sleep 1 秒中可以保证线程2和线程3交替获得锁并执行
pthread_mutex_unlock(&count_mutex);
sleep(1);
}//end for
pthread_exit(NULL);
}
void *watch_count(void *t) //检查条件变量
{
long my_id = (long)t;
printf("Starting watch_count(): thread %ld\n", my_id);
pthread_mutex_lock(&count_mutex);
while (count < COUNT_LIMIT)//这里用while防止虚假唤醒
{
printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
pthread_cond_wait(&count_threshold_cv, &count_mutex);//阻塞后自动释放锁
printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
printf("watch_count(): thread %ld Updating the value of count...\n", my_id,count);
count += 125;
printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
}
printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int i, rc;
long t1=1, t2=2, t3=3;
pthread_t threads[3];//3个线程
pthread_attr_t attr;//attr
/* 初始化 mutex 和 condition variable */
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init (&count_threshold_cv, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[0], &attr, watch_count, (void *)t1);//线程1关注count值
pthread_create(&threads[1], &attr, inc_count, (void *)t2);//线程2增加count值
pthread_create(&threads[2], &attr, inc_count, (void *)t3);//线程3增加count值
for (i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}//等待所有线程完成
printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n", NUM_THREADS, count);
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);
}
因篇幅问题不能全部显示,请点此查看更多更全内容