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

來源:互聯網
上載者:User

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

最近喜歡聽大學聽到的老歌,deutschland 德國世界盃時候流行的,據說不是主題曲但是比主題曲還要火。

本篇進入痛點了,mutex互斥鎖概念,mutex=mutual exclusion的縮寫,順便說一句:以前老師都愛用縮寫,也不跟同學說全稱,這尼瑪能理解深刻麼!下文是用法:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <pthread.h>//按規矩不能少  
  3.   
  4. using namespace std;  
  5.   
  6. #define NUM_THREADS 5  
  7.   
  8. int sum = 0;//定義個全域變數,讓所有線程進行訪問,這樣就會出現同時寫的情況,勢必會需要鎖機制;  
  9. pthread_mutex_t sum_mutex;  
  10.   
  11. void* say_hello(void* args)  
  12. {  
  13.     cout << "hello in thread " << *((int *)args) << endl;  
  14.     pthread_mutex_lock (&sum_mutex);//修改sum就先加鎖,鎖被佔用就阻塞,直到拿到鎖再修改sum;  
  15.     cout << "before sum is " << sum << " in thread " << *((int *)args) << endl;  
  16.     sum += *((int *)args);  
  17.     cout << "after sum is " << sum << " in thread " << *((int *)args) << endl;  
  18.     pthread_mutex_unlock (&sum_mutex);//完事後解鎖,釋放給其他線程使用;  
  19.   
  20.     pthread_exit(0);//退出隨便扔個狀態代碼  
  21. }  
  22.   
  23. int main()  
  24. {  
  25.     pthread_t tids[NUM_THREADS];  
  26.     int indexes[NUM_THREADS];  
  27.     //下三句是設定線程參數沒啥可說的  
  28.     pthread_attr_t attr;  
  29.     pthread_attr_init(&attr);  
  30.     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);  
  31.   
  32.     pthread_mutex_init (&sum_mutex, NULL);//這句是對鎖進行初始化,必須的;  
  33.   
  34.     for(int i = 0; i < NUM_THREADS; ++i)  
  35.     {  
  36.         indexes[i] = i;  
  37.         int ret = pthread_create( &tids[i], &attr, say_hello, (void *)&(indexes[i]) );//5個進程去你們去修改sum吧哈哈;  
  38.         if (ret != 0)  
  39.         {  
  40.            cout << "pthread_create error: error_code=" << ret << endl;  
  41.         }  
  42.     }  
  43.   
  44.     pthread_attr_destroy(&attr);//刪除參數變數  
  45.   
  46.     void *status;  
  47.         for (int i = 0; i < NUM_THREADS; ++i)  
  48.     {  
  49.                 int ret = pthread_join(tids[i], &status);  
  50.         if (ret != 0)  
  51.         {  
  52.             cout << "pthread_join error: error_code=" << ret << endl;  
  53.         }  
  54.     }  
  55.   
  56.     cout << "finally sum is " << sum << endl;  
  57.   
  58.     pthread_mutex_destroy(&sum_mutex);//登出鎖,可以看出使用pthread內建變數神馬的都對應了銷毀函數,估計是記憶體泄露相關的吧;  
  59. }  

慣例:g++ -lpthread -o ex_mutex ex_mutex.cpp

運行:

[cpp] view plaincopy
  1. hello in thread 4  
  2. before sum is 0 in thread 4  
  3. after sum is 4 in thread 4  
  4. hello in thread 3  
  5. before sum is 4 in thread 3  
  6. after sum is 7 in thread 3  
  7. hello in thread 2  
  8. before sum is 7 in thread 2  
  9. after sum is 9 in thread 2  
  10. hello in thread 1  
  11. before sum is 9 in thread 1  
  12. after sum is 10 in thread 1  
  13. hello in thread 0  
  14. before sum is 10 in thread 0  
  15. after sum is 10 in thread 0  
  16. finally sum is 10  

發現個現象,thread4先運行,很詭異吧而i是從0遞增的,所以呢多線程的順序是混亂的,混亂就是正常;只要sum訪問及修改是正常的,就達到多線程的目的了,運行順序不能作為參照;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.