雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

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

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

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

一:概述

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

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

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

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

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有