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

來源:互聯網
上載者:User

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

線程會建立了,如何線上程調用函數時,傳入參數呢?則應如下所示:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <pthread.h>  
  3.   
  4. using namespace std;  
  5.   
  6. #define NUM_THREADS 5  
  7.   
  8. void* say_hello(void* args)  
  9. {  
  10.     int i = *((int*)args);//對傳入的參數進行強制類型轉換,由無類型指標變為整形數指標,然後再讀取;  
  11.     cout << "hello in " << i << endl;  
  12. }  
  13.   
  14. int main()  
  15. {  
  16.     pthread_t tids[NUM_THREADS];  
  17.     cout << "hello in main..." << endl;  
  18.     for(int i = 0; i < NUM_THREADS; ++i)  
  19.     {  
  20.         int ret = pthread_create(&tids[i], NULL, say_hello, (void *)&i);//傳入的時候必須強制轉換為void* 類型,即無類型指標  
  21.         cout << "Current pthread id =" << tids[i] << endl;//這裡學會使用tids數組列印建立的進程id資訊;  
  22.         if (ret != 0)  
  23.         {  
  24.            cout << "pthread_create error: error_code=" << ret << endl;  
  25.         }  
  26.     }  
  27.   
  28.     pthread_exit(NULL);  
  29. }  

編譯、運行,結果如下:

[plain] view plaincopy
  1. Current pthread id =139671233451792  
  2. Current pthread id =139671222961936  
  3. Current pthread id =139671212472080  
  4. Current pthread id =139671201982224  
  5. Current pthread id =139671191492368  
  6. hello in 4196496  
  7. hello in 4196496  
  8. hello in 4196496  
  9. hello in 4196496  
  10. hello in 4196496  

是否發現了問題?對,i的值沒有輸出預想的結果,這是因為多線程造成的,主進程在i還未賦值時,線程已經開始跑啦!~

那麼下面代碼是正確的:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <pthread.h>  
  3.   
  4. using namespace std;  
  5.   
  6. #define NUM_THREADS 5  
  7.   
  8. void* say_hello(void* args)  
  9. {  
  10.     cout << "hello in thread " << *((int *)args) << endl;  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     pthread_t tids[NUM_THREADS];  
  16.     int indexes[NUM_THREADS];//用個數組來儲存i的值,就不會變了  
  17.   
  18.     for(int i = 0; i < NUM_THREADS; ++i)  
  19.     {  
  20.         indexes[i] = i;//先儲存i的值,在調用線程就不會出現問題了  
  21.         int ret = pthread_create( &tids[i], NULL, say_hello, (void *)&(indexes[i]) );  
  22.         if (ret != 0)  
  23.         {  
  24.            cout << "pthread_create error: error_code=" << ret << endl;  
  25.         }  
  26.     }  
  27.     for (int i = 0; i < NUM_THREADS; ++i)  
  28.         pthread_join(tids[i], NULL);  
  29. }  

編譯、運行:(來源程式去掉了列印線程id的廢話)

[plain] view plaincopy
    1. [cpp@node2 pthread]$ ./ex_create_args_ok  
    2. hello in thread 3  
    3. hello in thread 4  
    4. hello in thread 2  
    5. hello in thread 1  
    6. hello in thread 0 
相關文章

聯繫我們

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