pthread_cond 第三方库的线程同步机制
来源:互联网 发布:分布式锁 java 编辑:程序博客网 时间:2024/05/18 22:44
pthread_cond
条件变量 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 将唤醒等待改条件的所有线程。
下面的例子很简单的使用了 cond 。 使用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 将唤醒等待改条件的所有线程。
下面的例子很简单的使用了 cond 。 使用cond 我们可以比较高效的写出一个 线程池。
1 #include <pthread.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 pthread_mutex_t mutex;
5 pthread_cond_t cond;
6 int val = 0;
7 void *thread_zero_run(void *arg){
8 while(true){
9 pthread_mutex_lock(&mutex); // lock the mutex
10 while(val <= 2){ //condition. use while for double check
11 printf("thread_zero_run --> val:%d, wait for wake up\n", val);
12 pthread_cond_wait(&cond, &mutex);//call pthread_cond_wait
13 }
14 printf("therad_zero_run --> val:%d, zero it and unlock\n", val);
15 val = 0; //do sth
16 pthread_mutex_unlock(&mutex); //unlock the mutex
17 }
18 pthread_exit((void*)0);
19 }
20 void *thread_add_run(void *arg){
21 while(true){
22 pthread_mutex_lock(&mutex); //lock the mutex
23 ++val; //do sth
24 pthread_mutex_unlock(&mutex); //unlock the mutex
25 pthread_cond_signal(&cond); //wake up a therad whick is waiting for the cond
26 printf("after add val:%d and wake up one zero thread for check\n", val);
27 sleep(1);
28 }
29 pthread_exit((void*)0);
30 }
31 int main(){
32 pthread_t t_add, t_zero;
33 pthread_cond_init(&cond, NULL);
34 if(pthread_create(&t_add, NULL, thread_add_run, NULL)){
35 return 0;
36 }
37 if(pthread_create(&t_zero, NULL, thread_zero_run, NULL)){
38 return 0;
39 }
40 pthread_join(t_add, NULL);
41 pthread_join(t_zero, NULL);
42 return 0;
43 }
44
2 #include <unistd.h>
3 #include <stdio.h>
4 pthread_mutex_t mutex;
5 pthread_cond_t cond;
6 int val = 0;
7 void *thread_zero_run(void *arg){
8 while(true){
9 pthread_mutex_lock(&mutex); // lock the mutex
10 while(val <= 2){ //condition. use while for double check
11 printf("thread_zero_run --> val:%d, wait for wake up\n", val);
12 pthread_cond_wait(&cond, &mutex);//call pthread_cond_wait
13 }
14 printf("therad_zero_run --> val:%d, zero it and unlock\n", val);
15 val = 0; //do sth
16 pthread_mutex_unlock(&mutex); //unlock the mutex
17 }
18 pthread_exit((void*)0);
19 }
20 void *thread_add_run(void *arg){
21 while(true){
22 pthread_mutex_lock(&mutex); //lock the mutex
23 ++val; //do sth
24 pthread_mutex_unlock(&mutex); //unlock the mutex
25 pthread_cond_signal(&cond); //wake up a therad whick is waiting for the cond
26 printf("after add val:%d and wake up one zero thread for check\n", val);
27 sleep(1);
28 }
29 pthread_exit((void*)0);
30 }
31 int main(){
32 pthread_t t_add, t_zero;
33 pthread_cond_init(&cond, NULL);
34 if(pthread_create(&t_add, NULL, thread_add_run, NULL)){
35 return 0;
36 }
37 if(pthread_create(&t_zero, NULL, thread_zero_run, NULL)){
38 return 0;
39 }
40 pthread_join(t_add, NULL);
41 pthread_join(t_zero, NULL);
42 return 0;
43 }
44
0 0
- pthread_cond 第三方库的线程同步机制
- pthread_cond同步
- 线程互斥和条件同步,pthread_mutex和pthread_cond
- 线程的同步机制
- 线程的同步机制
- 线程的同步机制
- pthread_cond 线程条件变量
- linux的线程同步机制
- linux的线程同步机制
- linux的线程同步机制
- Linux的线程同步机制
- linux的线程同步机制
- linux的线程同步机制
- 线程间同步的机制
- linux的线程同步机制
- 线程的同步机制分析
- pthread_cond
- pthread_cond
- nothing
- flashcache在SLES 11 SP3上的编译错误
- URI和URL的区别
- Xilinx FPFA中LUT4_L 元件的使用
- 类加载器作用
- pthread_cond 第三方库的线程同步机制
- LA3700 Interesting Yang Hui Triangle(Lucas定理)
- 华为机试---找出随机数字中最大的三位数
- 修改LINUX目录或文件的权限和所有权
- 网页布局基础
- Windows 2012 管理 Deduplication 的磁盘注意事项2则
- java编程思想(第四版)第三章练习题-10
- sicily 1018之模拟题
- 给Android系统安装busybox