国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++程序中啟動線程的方法

2020-05-23 14:18:38
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C++程序中啟動線程的方法,作者針對C++11版本中的一些新特性進行了解說,需要的朋友可以參考下

C++11 引入一個全新的線程庫,包含啟動和管理線程的工具,提供了同步(互斥、鎖和原子變量)的方法,我將試圖為你介紹這個全新的線程庫。

如果你要編譯本文中的代碼,你至少需要一個支持 C++11 的編譯器,我使用的是 GCC 4.6.1,需要使用 -c++0x 或者 -c++11 參數來啟用 C++11 的支持。

啟動線程

在 C++11 中啟動一個線程是非常簡單的,你可以使用 std:thread 來創建一個線程實例,創建完會自動啟動,只需要給它傳遞一個要執行函數的指針即可,請看下面這個 Hello world 代碼:

 

 
  1. #include <thread> 
  2. #include <iostream> 
  3.  
  4. void hello(){ 
  5. std::cout << "Hello from thread " << std::endl; 
  6.  
  7. int main(){ 
  8. std::thread t1(hello); 
  9. t1.join(); 
  10.  
  11. return 0; 

所有跟線程相關的方法都在 thread 這個頭文件中定義,比較有意思的是我們在上面的代碼調用了 join() 函數,其目的是強迫主線程等待線程執行結束后才退出。如果你沒寫 join() 這行代碼,可能執行的結果是打印了 Hello from thread 和一個新行,也可能沒有新行。因為主線程可能在線程執行完畢之前就返回了。

線程標識

每個線程都有一個唯一的 ID 以識別不同的線程,std:thread 類有一個 get_id() 方法返回對應線程的唯一編號,你可以通過 std::this_thread 來訪問當前線程實例,下面的例子演示如何使用這個 id:

 

 
  1. #include <thread> 
  2. #include <iostream> 
  3. #include <vector> 
  4.  
  5. void hello(){ 
  6. std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl; 
  7.  
  8. int main(){ 
  9. std::vector<std::thread> threads; 
  10.  
  11. for(int i = 0; i < 5; ++i){ 
  12. threads.push_back(std::thread(hello)); 
  13.  
  14. for(auto& thread : threads){ 
  15. thread.join(); 
  16.  
  17. return 0; 

依次啟動每個線程,然后把它們保存到一個 vector 容器中,程序執行結果是不可預測的,例如:

 

 
  1. Hello from thread 140276650997504 
  2. Hello from thread 140276667782912 
  3. Hello from thread 140276659390208 
  4. Hello from thread 140276642604800 
  5. Hello from thread 140276676175616 

也可能是:

 

 
  1. Hello from thread Hello from thread Hello from thread 139810974787328Hello from thread 139810983180032Hello from thread 
  2. 139810966394624 
  3. 139810991572736 
  4. 139810958001920 

或者其他結果,因為多個線程的執行是交錯的。你完全沒有辦法去控制線程的執行順序(否則那還要線程干嗎?)

當線程要執行的代碼就一點點,你沒必要專門為之創建一個函數,你可以使用 lambda 來定義要執行的代碼,因此第一個例子我們可以改寫為:

 

 
  1. #include <thread> 
  2. #include <iostream> 
  3. #include <vector> 
  4.  
  5. int main(){ 
  6. std::vector<std::thread> threads; 
  7.  
  8. for(int i = 0; i < 5; ++i){ 
  9. threads.push_back(std::thread([](){ 
  10. std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl; 
  11. })); 
  12.  
  13. for(auto& thread : threads){ 
  14. thread.join(); 
  15.  
  16. return 0; 

在這里我們使用了一個 lambda 表達式替換函數指針,而結果是一樣的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盘山县| 新昌县| 凤冈县| 六安市| 个旧市| 名山县| 东乌| 涞源县| 周口市| 团风县| 松潘县| 怀来县| 静安区| 望奎县| 错那县| 廊坊市| 昌黎县| 古交市| 城口县| 绥阳县| 缙云县| 阿巴嘎旗| 横山县| 屏东市| 双流县| 武鸣县| 奇台县| 湖州市| 赞皇县| 若羌县| 平湖市| 定州市| 海淀区| 蕲春县| 葫芦岛市| 介休市| 静安区| 龙泉市| 象州县| 鄂温| 习水县|