日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

c+++11并发编程语言,C++11并发编程:多线程std:thread

發(fā)布時(shí)間:2025/3/11 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c+++11并发编程语言,C++11并发编程:多线程std:thread 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原標(biāo)題:C++11并發(fā)編程:多線程std:thread

一:概述

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

所需頭文件

二:構(gòu)造函數(shù)

1.默認(rèn)構(gòu)造函數(shù)

thread() noexcept

一個(gè)空的std::thread執(zhí)行對(duì)象

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

template

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

創(chuàng)建std::thread執(zhí)行對(duì)象,線程調(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對(duì)象不可拷貝構(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對(duì)象

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對(duì)象。

thread t1(threadFun);

thread::id threadId = t1.get_id();

cout << "線程ID:" << threadId << endl;

//threadId轉(zhuǎn)換成整形值,所需頭文件

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ù)會(huì)阻塞當(dāng)前線程,直到線程執(zhí)行完join才返回。

thread t1(threadFun);

t1.join() //阻塞等待

3.detach()

detach調(diào)用之后,目標(biāo)線程就成為了守護(hù)線程,駐留后臺(tái)運(yùn)行,與之關(guān)聯(lián)的std::thread對(duì)象失去對(duì)目標(biāo)線程的關(guān)聯(lián),無法再通過std::thread對(duì)象取得該線程的控制權(quán)。

4.swap()

交換兩個(gè)線程對(duì)象

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

獲得邏輯處理器儲(chǔ)量,返回值為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 做變量傳遞時(shí)并不是引用,而是對(duì)變量做了拷貝,所以在傳遞給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 = "我是一個(gè)類的成員函數(shù)!";

thread t1(&Object::fun, &obj, str);

t1.join();

getchar();

return 1;

---------------------

作者:蝸牛201

原文:https://blog.csdn.net/woniu211111/article/details/85123281

責(zé)任編輯:

總結(jié)

以上是生活随笔為你收集整理的c+++11并发编程语言,C++11并发编程:多线程std:thread的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。