免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2000 | 回复: 3
打印 上一主题 下一主题

关于pthread_cond_wait [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-02 13:28 |只看该作者 |倒序浏览
  1. void decrement_count(){
  2.         pthread_mutex_lock (&count_lock);
  3.         while(count==0)
  4.         {
  5.                 cout<<"++pthread_cond_wait...\n";
  6.                 pthread_cond_wait( &count_nonzero, &count_lock);
  7.                 cout<<"--pthread_cond_wait...\n";
  8.         }
  9.         cout<<"退出while循环了.\n";
  10.         count=count -1;
  11.         cout<<__LINE__<<endl;
  12.         pthread_mutex_unlock (&count_lock);
  13. }
  14. void increment_count(){
  15.         cout<<__LINE__<<endl;
  16.         pthread_mutex_lock(&count_lock);
  17.         cout<<__LINE__<<endl;
  18.         if(count==0)
  19.         {
  20.                 cout<<"++pthread_cond_signal\n";
  21.                 pthread_cond_signal(&count_nonzero);
  22.                 cout<<"--pthread_cond_signal\n";
  23.         }
  24.         count=count+1;
  25.         cout<<__LINE__<<endl;
  26.         pthread_mutex_unlock(&count_lock);
  27. }
复制代码
各位大大好!
    我想请问下,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...
......
一直持续地运行呢?
希望各位不吝赐教,先谢过了!

论坛徽章:
0
2 [报告]
发表于 2010-09-02 13:29 |只看该作者
我把全部代码都贴出来算了。。
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <pthread.h>
  4. using namespace std;
  5. pthread_mutex_t count_lock;
  6. pthread_cond_t count_nonzero;
  7. unsigned count;
  8. void decrement_count();
  9. void increment_count();
  10. void thread1(void)
  11. {
  12.         cout<<"This is a thread1.\n";
  13.         decrement_count();
  14. }
  15. void thread2(void)
  16. {
  17.         cout<<"This is a thread1.\n";
  18.         increment_count();
  19. }
  20. void decrement_count(){
  21.         pthread_mutex_lock (&count_lock);
  22.         while(count==0)
  23.         {
  24.                 cout<<"++pthread_cond_wait...\n";
  25.                 pthread_cond_wait( &count_nonzero, &count_lock);
  26.                 cout<<"--pthread_cond_wait...\n";
  27.         }
  28.         cout<<"退出while循环了.\n";
  29.         count=count -1;
  30.         cout<<__LINE__<<endl;
  31.         pthread_mutex_unlock (&count_lock);
  32. }
  33. void increment_count(){
  34.         cout<<__LINE__<<endl;
  35.         pthread_mutex_lock(&count_lock);
  36.         cout<<__LINE__<<endl;
  37.         if(count==0)
  38.         {
  39.                 cout<<"++pthread_cond_signal\n";
  40.                 pthread_cond_signal(&count_nonzero);
  41.                 cout<<"--pthread_cond_signal\n";
  42.         }
  43.         count=count+1;
  44.         cout<<__LINE__<<endl;
  45.         pthread_mutex_unlock(&count_lock);
  46. }

  47. int main(void)
  48. {
  49.         pthread_t id,id2;
  50.         int i,ret;
  51.         count=0;
  52.         pthread_cond_init(&count_nonzero,NULL);
  53.         ret=pthread_create(&id,NULL,(void *(*)(void *))thread1,NULL);
  54.         if(ret!=0){
  55.                 cout<<"Create pthread error!\n";
  56.                 return (1);
  57.         }
  58.         sleep(1);
  59.         ret=pthread_create(&id2,NULL,(void *(*)(void *))thread2,NULL);
  60.         if(ret!=0){
  61.                 cout<<"Create pthread error!\n";
  62.                 return (1);
  63.         }
  64.         pthread_join(id,NULL);
  65.         pthread_join(id2,NULL);
  66.         return (0);
  67. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-09-02 19:35 |只看该作者
我直接拷贝你的程序 运行结果是

This is a thread1.
++pthread_cond_wait...
This is a thread1.
34
36
++pthread_cond_signal
--pthread_cond_signal
44
--pthread_cond_wait...
退出while循环了.
30

论坛徽章:
0
4 [报告]
发表于 2010-09-03 11:52 |只看该作者
本帖最后由 ydfgic 于 2010-09-03 11:55 编辑

你的互斥量初始化了没?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP