c/c++: 多線程編程基礎講解(六)

來源:互聯網
上載者:User

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

上篇說了下互斥量的用法,今兒說一下條件訊號量的用法,這兩種多線程變數的用法其實取決於情景,需要體會,見文:

[cpp] view plaincopy
  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. }  
[cpp] view plaincopy
  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

執行結果:

[cpp] view plaincopy
    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 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.