C++11并發(fā)編程:多線程std::thread
一:概述
C++11引入了thread類,大大降低了多線程使用的復(fù)雜度,原先使用多線程只能用系統(tǒng)的API,無法解決跨平臺問題,一套代碼平臺移植,對應(yīng)多線程代碼也必須要修改。現(xiàn)在在C++11中只需使用語言層面的thread可以解決這個問題。
所需頭文件<thread>
二:構(gòu)造函數(shù)
1.默認構(gòu)造函數(shù)
- thread() noexcept
 - 一個空的std::thread執(zhí)行對象
 
2.初始化構(gòu)造函數(shù)
template<class Fn, class... Args>
explicit thread(Fn&& fn, Args&&... args);
創(chuàng)建std::thread執(zhí)行對象,線程調(diào)用threadFun函數(shù),函數(shù)參數(shù)為args。
void threadFun(int a)
{
  cout << "this is thread fun !" << endl;
}
thread t1(threadFun, 2);
3.拷貝構(gòu)造函數(shù)
thread(const thread&) = delete;
拷貝構(gòu)造函數(shù)被禁用,std::thread對象不可拷貝構(gòu)造
void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
}  
int value = 2;
thread t1(threadFun, std::ref(value));
4.Move構(gòu)造函數(shù)
thread(thread&& x)noexcept
調(diào)用成功原來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轉(zhuǎn)換成整形值,所需頭文件<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ù),調(diào)用該函數(shù)會阻塞當前線程,直到線程執(zhí)行完join才返回。
thread t1(threadFun); t1.join() //阻塞等待
3.detach()
detach調(diào)用之后,目標線程就成為了守護線程,駐留后臺運行,與之關(guān)聯(lián)的std::thread對象失去對目標線程的關(guān)聯(lián),無法再通過std::thread對象取得該線程的控制權(quán)。
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 << "構(gòu)造函數(shù)" << endl;
  }
  ~Object()
  {
    cout << "析構(gòu)函數(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;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
上一篇:C++中指針函數(shù)與函數(shù)指針的使用
欄 目:C語言
下一篇:C++構(gòu)造函數(shù)和析構(gòu)函數(shù)的使用與講解
本文標題:C++11并發(fā)編程:多線程std::thread
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/584.html
您可能感興趣的文章
- 01-10異步http listener 完全并發(fā)處理懲罰http懇求的小例子
 - 01-10深入理解C/C++混合編程
 - 01-10深入sizeof的使用詳解
 - 01-10C語言編程時常犯十八個錯誤小結(jié)
 - 01-10通過c++11改進我們的模式之改進命令模式
 - 01-10linux c多線程編程實例代碼
 - 01-10c語言socket多線程編程限制客戶端連接數(shù)
 - 01-10C語言socket編程開發(fā)應(yīng)用示例
 - 01-10c語言網(wǎng)絡(luò)編程-標準步驟(改進版)
 - 01-10c語言網(wǎng)絡(luò)編程-標準步驟(比較簡單)
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機閱讀
- 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實例總結(jié)
 


