pthread_cond
来源:互联网 发布:mac ai2017cc 破解 编辑:程序博客网 时间:2024/06/06 11:45
条件变量 pthread_cond, 另外一种线程间的同步机制。普通的 mutex 只允许一个线程进入临界区,就是拿到mutex这把锁的线程,而cond 允许多个线程同时进入临界区,由它来控制,在某些条件成立的时候,来唤醒其中一个等待着的线程,或者是唤醒所有等待着的线程。
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
int pthread_cond_timewait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* tout)
传递给pthrad_cond_wait 的互斥量 mutex 对条件进行保护,调用者把锁住的互斥量传递给pthread_cond_wait 函数,函数把调用线程放到等待条件的线程列表里面,然后对互斥量解锁,当pthread_cond_wait 返回的时候,互斥量再次被锁住。函数 pthread_cond_timewait 与 pthread_cond_wait 差不多,只不过是多了一个超时时间的限制。
两个函数调用成功返回的时候,需要重新检查条件,因为其他线程可能更改了条件。
int pthread_cond_signal(pthread_cond_t* cond);
int pthread_cond_broadcast(pthread_cond_t* cond);
pthread_cond_signal 函数将唤醒等待该条件的某个线程,pthread_cond_broadcast 将唤醒等待改条件的所有线程。
输出:
转自:http://www.cppblog.com/MemoryGarden/archive/2011/06/08/148241.html
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
int pthread_cond_timewait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* tout)
传递给pthrad_cond_wait 的互斥量 mutex 对条件进行保护,调用者把锁住的互斥量传递给pthread_cond_wait 函数,函数把调用线程放到等待条件的线程列表里面,然后对互斥量解锁,当pthread_cond_wait 返回的时候,互斥量再次被锁住。函数 pthread_cond_timewait 与 pthread_cond_wait 差不多,只不过是多了一个超时时间的限制。
两个函数调用成功返回的时候,需要重新检查条件,因为其他线程可能更改了条件。
int pthread_cond_signal(pthread_cond_t* cond);
int pthread_cond_broadcast(pthread_cond_t* cond);
pthread_cond_signal 函数将唤醒等待该条件的某个线程,pthread_cond_broadcast 将唤醒等待改条件的所有线程。
下面的例子很简单的使用了 cond 。 使用cond 我们可以比较高效的写出一个 线程池。
#include <pthread.h>#include <unistd.h>#include <stdio.h>pthread_mutex_t mutex;pthread_cond_t cond;int val = 0;void *thread_zero_run(void *arg){while(1){pthread_mutex_lock(&mutex); // lock the mutexwhile(val <= 2){ //condition. use while for double checkprintf("thread_zero_run --> val:%d, wait for wake up\n", val);pthread_cond_wait(&cond, &mutex);//call pthread_cond_wait}printf("therad_zero_run --> val:%d, zero it and unlock\n", val);val = 0; //do sthpthread_mutex_unlock(&mutex); //unlock the mutex} pthread_exit((void*)0);}void *thread_add_run(void *arg){while(1){pthread_mutex_lock(&mutex); //lock the mutex ++val; //do sth pthread_mutex_unlock(&mutex); //unlock the mutex pthread_cond_signal(&cond); //wake up a therad whick is waiting for the condprintf("after add val:%d and wake up one zero thread for check\n", val);sleep(1);} pthread_exit((void*)0);}int main(){pthread_t t_add, t_zero; pthread_cond_init(&cond, NULL); if(pthread_create(&t_add, NULL, thread_add_run, NULL)){ return 0; } if(pthread_create(&t_zero, NULL, thread_zero_run, NULL)){ return 0; } pthread_join(t_add, NULL); pthread_join(t_zero, NULL); return 0;}
输出:
after add val:1 and wake up one zero thread for checkthread_zero_run --> val:1, wait for wake upafter add val:2 and wake up one zero thread for checkthread_zero_run --> val:2, wait for wake upafter add val:3 and wake up one zero thread for checktherad_zero_run --> val:3, zero it and unlockthread_zero_run --> val:0, wait for wake upafter add val:1 and wake up one zero thread for checkthread_zero_run --> val:1, wait for wake upafter add val:2 and wake up one zero thread for checkthread_zero_run --> val:2, wait for wake upafter add val:3 and wake up one zero thread for checktherad_zero_run --> val:3, zero it and unlockthread_zero_run --> val:0, wait for wake upafter add val:1 and wake up one zero thread for checkthread_zero_run --> val:1, wait for wake upafter add val:2 and wake up one zero thread for checkthread_zero_run --> val:2, wait for wake upafter add val:3 and wake up one zero thread for checktherad_zero_run --> val:3, zero it and unlockthread_zero_run --> val:0, wait for wake up
转自:http://www.cppblog.com/MemoryGarden/archive/2011/06/08/148241.html
- pthread_cond
- pthread_cond
- pthread_cond同步
- pthread_cond 应用
- pthread_cond 线程条件变量
- pthread_cond 类函数
- pthread pthread_mutex pthread_cond相关
- pthread_cond讲解及实例
- pthread pthread_mutex pthread_cond相关
- 条件变量 pthread_cond
- pthread_mutex_t and pthread_cond 混合使用
- Linux 条件变量的使用pthread_cond
- pthread_cond 第三方库的线程同步机制
- 详解linux互斥锁 pthread_mutex和条件变量pthread_cond
- 线程互斥和条件同步,pthread_mutex和pthread_cond
- POSIX---互斥锁(pthread_mutex)和条件变量(pthread_cond)的用法
- 【那些年遇到过的面试题】pthread_cond
- gcc 编译错误:pthread_cond.c:62: error: stray ‘\343’ in program
- VC控制台颜色设置
- 泛型的单例模式
- mysql master slave 主从安装
- 怎样用hibernate的hql查询查询成map或list
- springAOP
- pthread_cond
- c#对xml操作小结
- 新型的按键扫描程序(备用)
- 使用DBCC CHECKPRIMARYFILE 查询Detach数据库信息
- iptables命令
- android 用Achartengine 作图
- UNIX:在父子进程间用管道技术通信
- <推荐>参加国际会议海报制作(poster)的Latex模板
- 32位Ubuntu 11.10下android2.3.7源码下载与编译小结