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

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

C++11并發編程:多線程std::thread

2020-01-26 13:36:28
字體:
來源:轉載
供稿:網友

一:概述

C++11引入了thread類,大大降低了多線程使用的復雜度,原先使用多線程只能用系統的API,無法解決跨平臺問題,一套代碼平臺移植,對應多線程代碼也必須要修改。現在在C++11中只需使用語言層面的thread可以解決這個問題。

所需頭文件<thread>

二:構造函數

1.默認構造函數

  • thread() noexcept
  • 一個空的std::thread執行對象

2.初始化構造函數

template<class Fn, class... Args>

explicit thread(Fn&& fn, Args&&... args);

創建std::thread執行對象,線程調用threadFun函數,函數參數為args。

void threadFun(int a){  cout << "this is thread fun !" << endl;}thread t1(threadFun, 2);

3.拷貝構造函數

thread(const thread&) = delete;

拷貝構造函數被禁用,std::thread對象不可拷貝構造

void threadFun(int& a){  cout << "this is thread fun !" << endl;}  int value = 2;thread t1(threadFun, std::ref(value));

4.Move構造函數

thread(thread&& x)noexcept

調用成功原來x不再是std::thread對象

void threadFun(int& a){  cout << "this is thread fun !" << endl;} int value = 2;thread t1(threadFun, std::ref(value));thread t2(std::move(t1));t2.join();

三:成員函數

1.get_id()

獲取線程ID,返回類型std::thread::id對象。

thread t1(threadFun);thread::id threadId = t1.get_id();cout << "線程ID:" << threadId << endl;//threadId轉換成整形值,所需頭文件<sstream>ostringstream  oss;oss << t1.get_id();string strId = oss.str();unsigned long long tid = stoull(strId);cout << "線程ID:" << tid << endl;

2.join()

創建線程執行線程函數,調用該函數會阻塞當前線程,直到線程執行完join才返回。

thread t1(threadFun);t1.join() //阻塞等待

3.detach()

detach調用之后,目標線程就成為了守護線程,駐留后臺運行,與之關聯的std::thread對象失去對目標線程的關聯,無法再通過std::thread對象取得該線程的控制權。

4.swap()

交換兩個線程對象

thread t1(threadFun1);thread t2(threadFun2);cout << "線程1的ID:" << t1.get_id() << endl;cout << "線程2的ID:" << t2.get_id() << endl;t1.swap(t2);cout << "線程1的ID:" << t1.get_id() << endl;cout << "線程2的ID:" << t2.get_id() << endl;

5.hardware_concurrency()

獲得邏輯處理器儲量,返回值為int型

int coreNum = thread::hardware_concurrency();

四:使用

1.創建線程

void threadFun1(){  cout << "this is thread fun1 !" << endl;}int main(){  thread t1(threadFun1);  t1.join();  getchar();  return 1;}

2.創建線程,傳參

void threadFun1(int v){  cout << "this is thread fun1 !" << endl;  cout << v << endl;}int main(){  int value = 6;  thread t1(threadFun1, value);  t1.join();  getchar();  return 1;}

需要注意,變量int value 和int v 做變量傳遞時并不是引用,而是對變量做了拷貝,所以在傳遞給int v前,int value不能出作用域(釋放了內存),join(),可以保證int value變量釋放內存,如果使用detach(),可能存在這種情況。

3.創建線程,引用傳參

void threadFun1(int& v){  cout << "this is thread fun1 !" << endl;  cout << v << endl;}int main(){  int value = 6;  thread t1(threadFun1, std::ref(value));  t1.join();  getchar();  return 1;}

4.創建建線程,線程函數為類成員函數

class Object{public:  Object()  {    cout << "構造函數" << endl;  }  ~Object()  {    cout << "析構函數" << endl;  }  void fun(string info)  {    cout << info << endl;  }};int main(){  Object obj;  string str = "我是一個類的成員函數!";  thread t1(&Object::fun, &obj, str);  t1.join();  getchar();  return 1;}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长垣县| 汉阴县| 霍州市| 玉田县| 古交市| 稻城县| 开远市| 靖西县| 晋州市| 通渭县| 开平市| 乡城县| 增城市| 大渡口区| 壤塘县| 大姚县| 进贤县| 和龙市| 盐山县| 天全县| 敦化市| 彭水| 佛冈县| 辽源市| 乐至县| 溆浦县| 拉萨市| 株洲县| 成都市| 封丘县| 勃利县| 江阴市| 台安县| 恭城| 勐海县| 新绛县| 兴化市| 南靖县| 揭东县| 班玛县| 北碚区|