+ -
当前位置:首页 → 问答吧 → 关于pthread_cond_wait

关于pthread_cond_wait

时间:2010-09-02

来源:互联网

  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...
......
一直持续地运行呢?
希望各位不吝赐教,先谢过了!

作者: huangbt_unix   发布时间: 2010-09-02

我把全部代码都贴出来算了。。
  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. }
复制代码

作者: huangbt_unix   发布时间: 2010-09-02

相关阅读 更多

热门下载

更多