欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++11并發(fā)編程:多線程std::thread

 更新時間:2018年12月21日 08:35:38   作者:蝸牛201  
今天小編就為大家分享一篇關于C++11并發(fā)編程:多線程std::thread,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

一:概述

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

所需頭文件<thread>

二:構造函數(shù)

1.默認構造函數(shù)

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

2.初始化構造函數(shù)

template<class Fn, class... Args>

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

創(chuàng)建std::thread執(zhí)行對象,線程調用threadFun函數(shù),函數(shù)參數(shù)為args。

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

3.拷貝構造函數(shù)

thread(const thread&) = delete;

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

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

4.Move構造函數(shù)

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();

三:成員函數(shù)

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()

創(chuàng)建線程執(zhí)行線程函數(shù),調用該函數(shù)會阻塞當前線程,直到線程執(zhí)行完join才返回。

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

3.detach()

detach調用之后,目標線程就成為了守護線程,駐留后臺運行,與之關聯(lián)的std::thread對象失去對目標線程的關聯(lián),無法再通過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.創(chuàng)建線程

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

2.創(chuàng)建線程,傳參

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不能出作用域(釋放了內(nèi)存),join(),可以保證int value變量釋放內(nèi)存,如果使用detach(),可能存在這種情況。

3.創(chuàng)建線程,引用傳參

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.創(chuàng)建建線程,線程函數(shù)為類成員函數(shù)

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

總結

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

相關文章

  • 剖析C++編程當中指針作為函數(shù)參數(shù)的用法

    剖析C++編程當中指針作為函數(shù)參數(shù)的用法

    這篇文章主要介紹了剖析C++編程當中指針作為函數(shù)參數(shù)的用法,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • C++實現(xiàn)簡單版圖書管理系統(tǒng)

    C++實現(xiàn)簡單版圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)簡單版圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 基于QT5實現(xiàn)一個時鐘桌面

    基于QT5實現(xiàn)一個時鐘桌面

    這篇文章主要介紹了利用QT5實現(xiàn)的一個時鐘桌面,文中的示例代碼講解詳細,對我們學習或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2022-01-01
  • C語言編程動態(tài)內(nèi)存分配常見錯誤全面分析

    C語言編程動態(tài)內(nèi)存分配常見錯誤全面分析

    這篇文章主要介紹了C語言編程中動態(tài)內(nèi)存分配的常見錯誤全面分析講解,同樣遇到過C語言動態(tài)內(nèi)存分配各種問題的同學可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Qt實戰(zhàn)之實現(xiàn)圖片瀏覽器

    Qt實戰(zhàn)之實現(xiàn)圖片瀏覽器

    這篇文章主要為大家詳細介紹了如何利用Qt實現(xiàn)簡易的圖片瀏覽器,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • C++實現(xiàn)單鏈表的構造

    C++實現(xiàn)單鏈表的構造

    這篇文章主要為大家詳細介紹了C++實現(xiàn)單鏈表的構造,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • c++將數(shù)組名作為函數(shù)參數(shù)對數(shù)組元素進行相應的運算

    c++將數(shù)組名作為函數(shù)參數(shù)對數(shù)組元素進行相應的運算

    這篇文章主要介紹了c++將數(shù)組名作為函數(shù)參數(shù)對數(shù)組元素進行相應的運算,需要的朋友可以參考下
    2014-05-05
  • C++中的Switch 語句詳情

    C++中的Switch 語句詳情

    在日常的開發(fā)當中,我們經(jīng)常會遇到一種情況,我們用一個變量表示狀態(tài)。比如關閉-激活-完成,當我們需要判斷狀態(tài)的時候,就需要羅列if-else語句。今天這篇文章我們就來介紹一下C++ Switch 語句,需要的朋友可以參考一下
    2021-11-11
  • C語言實現(xiàn)俄羅斯方塊源代碼

    C語言實現(xiàn)俄羅斯方塊源代碼

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)俄羅斯方塊的源代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • C和C++中argc和argv的含義及用法詳解

    C和C++中argc和argv的含義及用法詳解

    argv 是 argument vector的縮寫,表示傳入main函數(shù)的參數(shù)序列或指針,這篇文章主要介紹了C和C++中argc和argv的含義以及用法,需要的朋友可以參考下
    2022-11-11

最新評論