欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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

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

啟動線程

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

 

#include <thread>
#include <iostream>
 
void hello(){
  std::cout << "Hello from thread " << std::endl;
}
 
int main(){
  std::thread t1(hello);
  t1.join();
 
  return 0;
}


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

線程標識

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

#include <thread>
#include <iostream>
#include <vector>
 
void hello(){
  std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}
 
int main(){
  std::vector<std::thread> threads;
 
  for(int i = 0; i < 5; ++i){
    threads.push_back(std::thread(hello));
  }
 
  for(auto& thread : threads){
    thread.join();
  }
 
  return 0;
}


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

  Hello from thread 140276650997504
  Hello from thread 140276667782912
  Hello from thread 140276659390208
  Hello from thread 140276642604800
  Hello from thread 140276676175616

也可能是:

  Hello from thread Hello from thread Hello from thread 139810974787328Hello from thread 139810983180032Hello from thread
  139810966394624
  139810991572736
  139810958001920

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


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

#include <thread>
#include <iostream>
#include <vector>
 
int main(){
  std::vector<std::thread> threads;
 
  for(int i = 0; i < 5; ++i){
    threads.push_back(std::thread([](){
      std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
    }));
  }
 
  for(auto& thread : threads){
    thread.join();
  }
 
  return 0;
}

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

上一篇:C語言簡單實現計算字符個數的方法

欄    目:C語言

下一篇:探究在C++程序并發(fā)時保護共享數據的問題

本文標題:C++程序中啟動線程的方法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2975.html

網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有