关于pthread_cond_wait
时间:2010-09-02
来源:互联网
- void decrement_count(){
- pthread_mutex_lock (&count_lock);
- while(count==0)
- {
- cout<<"++pthread_cond_wait...\n";
- pthread_cond_wait( &count_nonzero, &count_lock);
- cout<<"--pthread_cond_wait...\n";
- }
- cout<<"退出while循环了.\n";
- count=count -1;
- cout<<__LINE__<<endl;
- pthread_mutex_unlock (&count_lock);
- }
- void increment_count(){
- cout<<__LINE__<<endl;
- pthread_mutex_lock(&count_lock);
- cout<<__LINE__<<endl;
- if(count==0)
- {
- cout<<"++pthread_cond_signal\n";
- pthread_cond_signal(&count_nonzero);
- cout<<"--pthread_cond_signal\n";
- }
- count=count+1;
- cout<<__LINE__<<endl;
- pthread_mutex_unlock(&count_lock);
- }
我想请问下,pthread_cond_wait不是会阻塞在条件变量count_nonzero上吗,那这样的话++pthread_cond_wait...应该只输出一次才对,
既然这样,为什么实际运行的时候是:
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
--pthread_cond_wait...
++pthread_cond_wait...
......
一直持续地运行呢?
希望各位不吝赐教,先谢过了!
作者: huangbt_unix 发布时间: 2010-09-02
我把全部代码都贴出来算了。。
复制代码
- #include <stdio.h>
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- pthread_mutex_t count_lock;
- pthread_cond_t count_nonzero;
- unsigned count;
- void decrement_count();
- void increment_count();
- void thread1(void)
- {
- cout<<"This is a thread1.\n";
- decrement_count();
- }
- void thread2(void)
- {
- cout<<"This is a thread1.\n";
- increment_count();
- }
- void decrement_count(){
- pthread_mutex_lock (&count_lock);
- while(count==0)
- {
- cout<<"++pthread_cond_wait...\n";
- pthread_cond_wait( &count_nonzero, &count_lock);
- cout<<"--pthread_cond_wait...\n";
- }
- cout<<"退出while循环了.\n";
- count=count -1;
- cout<<__LINE__<<endl;
- pthread_mutex_unlock (&count_lock);
- }
- void increment_count(){
- cout<<__LINE__<<endl;
- pthread_mutex_lock(&count_lock);
- cout<<__LINE__<<endl;
- if(count==0)
- {
- cout<<"++pthread_cond_signal\n";
- pthread_cond_signal(&count_nonzero);
- cout<<"--pthread_cond_signal\n";
- }
- count=count+1;
- cout<<__LINE__<<endl;
- pthread_mutex_unlock(&count_lock);
- }
-
- int main(void)
- {
- pthread_t id,id2;
- int i,ret;
- count=0;
- pthread_cond_init(&count_nonzero,NULL);
- ret=pthread_create(&id,NULL,(void *(*)(void *))thread1,NULL);
- if(ret!=0){
- cout<<"Create pthread error!\n";
- return (1);
- }
- sleep(1);
- ret=pthread_create(&id2,NULL,(void *(*)(void *))thread2,NULL);
- if(ret!=0){
- cout<<"Create pthread error!\n";
- return (1);
- }
- pthread_join(id,NULL);
- pthread_join(id2,NULL);
- return (0);
- }
作者: huangbt_unix 发布时间: 2010-09-02
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28