c/c++: 多线程编程基础讲解(六)

http://blog.csdn.net/lzx_bupt/article/details/6915117

上篇说了下互斥量的用法,今儿说一下条件信号量的用法,这两种多线程变量的用法其实取决于情景,需要体会,见文:

  1. #include <iostream>  
  2. #include <pthread.h>//带头文件  
  3. #include <stdio.h>  
  4.   
  5. using namespace std;  
  6.   
  7. #define BOUNDARY 5  
  8.   
  9. int tasks = 10;  
  10.   
  11. pthread_mutex_t tasks_mutex;//因为两个线程要修改一个全局变量,需要互斥量;  
  12. pthread_cond_t tasks_cond;//因为两个线程间有条件关系:当tasks>5时,hello2处理它,处理一次减少1;反之hello1处理,直到tasks减为零;  
  13.   
  14. void* say_hello2(void* args)//hello2处理函数  
  15. {  
  16.     pthread_t pid = pthread_self();//打印当前线程id便于跟踪  
  17.     cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;  
  18.   
  19.     bool is_signaled = false;//随便一个标志位  
  20.     while(1)//无限循环  
  21.     {  
  22.         pthread_mutex_lock(&tasks_mutex);//要修改了,加锁  
  23.         if (tasks > BOUNDARY)//>5才修改  
  24.         {  
  25.             cout << "["<< pid << "] take task:  "<< tasks << " in thread "<< *((int*)args) << endl;  
  26.             --tasks;//减少1  
  27.         }  
  28.         else if (!is_signaled)  
  29.         {  
  30.             cout << "["<< pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;  
  31.             pthread_cond_signal(&tasks_cond);//表明已经不是>5了告诉hello1进程去处理:发送信号;  
  32.             is_signaled = true;//表示信号已经发送了  
  33.         }  
  34.         pthread_mutex_unlock(&tasks_mutex);//操作完解锁  
  35.   
  36.         if (tasks == 0) break;//必须等待tasks全部减为零即hello1完成操作,才跳出循环结束这个进程  
  37.     }  
  38. }  
  1. <p>void* say_hello1(void* args)//<=5处理函数  
  2. {  
  3.     pthread_t pid = pthread_self();  
  4.     cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;</p><p>    while(1)  
  5.     {  
  6.         pthread_mutex_lock(&tasks_mutex);  
  7.         if (tasks > BOUNDARY)//如果>5说明需要hello2处理,那么该线程就需要等待  
  8.         {  
  9.             cout << "["<< pid << "] pthread_cond_wait in thread " << *((int*)args) << endl;  
  10.             pthread_cond_wait(&tasks_cond, &tasks_mutex);//等待信号量生效,当hello2发出信号,这里就跳出wait,执行后续;  
  11.         }  
  12.         else  
  13.         {  
  14.             cout << "["<< pid << "] take task:  "<< tasks << " in thread "<< *((int*)args) << endl;  
  15.             --tasks;//<=5就--  
  16.         }  
  17.         pthread_mutex_unlock(&tasks_mutex);</p><p>        if (tasks == 0) break;//为零时退出,同hello2一样  
  18.     }</p><p>}</p><p>int main()  
  19. {  
  20.     pthread_attr_t attr;//线程创建为joinable的,使得主进程可以和两个线程同步,两个线程完成工作退出后,主进程再退出;  
  21.     pthread_attr_init(&attr);  
  22.     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);</p><p>    pthread_mutex_init(&tasks_mutex, NULL);//初始化互斥量  
  23.     pthread_cond_init(&tasks_cond, NULL);//初始化条件信号量</p><p>    pthread_t tid1, tid2;//用于保存两个线程的id号  
  24.     int index1 = 1;  
  25.     int ret = pthread_create( &tid1, &attr, say_hello1, (void *)&index1);  
  26.     if (ret != 0)  
  27.     {  
  28.         cout << "pthread_create error: error_code=" << ret << endl;  
  29.     }</p><p>    int index2 = 2;  
  30.     ret = pthread_create( &tid2, &attr, say_hello2, (void *)&index2);  
  31.     if (ret != 0)  
  32.     {  
  33.         cout << "pthread_create error: error_code=" << ret << endl;  
  34.     }</p><p> </p><p>    pthread_join(tid1, NULL);//连接两个线程  
  35.     pthread_join(tid2, NULL);</p><p>    pthread_attr_destroy(&attr);//该销毁的销毁  
  36.     pthread_mutex_destroy(&tasks_mutex);  
  37.     pthread_cond_destroy(&tasks_cond);</p><p>    //正常退出  
  38. }</p><p> </p>  

惯例:g++ -lpthread -o ex_cond ex_cond.cpp

执行结果:

    1. [cpp@node2 pthread]$ ./ex_cond   
    2. [140009886947088] hello in thread 2  
    3. [140009886947088] take task:  10 in thread 2  
    4. [140009886947088] take task:  9 in thread 2  
    5. [140009886947088] take task:  8 in thread 2  
    6. [140009886947088] take task:  7 in thread 2  
    7. [140009886947088] take task:  6 in thread 2  
    8. [140009886947088] pthread_cond_signal in thread 2  
    9. [140009897436944] hello in thread 1  
    10. [140009897436944] take task:  5 in thread 1  
    11. [140009897436944] take task:  4 in thread 1  
    12. [140009897436944] take task:  3 in thread 1  
    13. [140009897436944] take task:  2 in thread 1  
    14. [140009897436944] take task:  1 in thread 1 
posted @ 2012-09-04 12:16  董雨  阅读(423)  评论(0编辑  收藏  举报