c++11之std::async 和std::thread的區(qū)別小結(jié)
std::async和std::thread都是C++11中提供的線程庫(kù),它們都可以用于創(chuàng)建新線程。它們的主要區(qū)別在于:
- std::async有時(shí)候并不創(chuàng)建新線程,而是使用線程池中的線程來(lái)執(zhí)行任務(wù),這取決于實(shí)現(xiàn)。而std::thread總是創(chuàng)建新線程。
- std::async返回一個(gè)std::future對(duì)象,可以用來(lái)獲取異步任務(wù)的結(jié)果。而std::thread沒(méi)有返回值,需要通過(guò)其他方式來(lái)獲取線程執(zhí)行的結(jié)果。
- std::async可以指定任務(wù)的執(zhí)行策略,例如std::launch::async表示一定要?jiǎng)?chuàng)建新線程執(zhí)行任務(wù),std::launch::deferred表示可以不創(chuàng)建新線程,等到調(diào)用std::future的get()方法時(shí)再執(zhí)行任務(wù)。而std::thread沒(méi)有這樣的選項(xiàng)。
#include <iostream>
#include <future>
#include <thread>
int task()
{
std::cout << "Task is running in thread " << std::this_thread::get_id() << std::endl;
return 42;
}
int main()
{
// 使用std::async創(chuàng)建異步任務(wù)
std::future<int> f1 = std::async(std::launch::async, task);
std::cout << "Async task has been started." << std::endl;
// 使用std::thread創(chuàng)建新線程
std::thread t(task);
std::cout << "Thread has been started." << std::endl;
// 等待異步任務(wù)完成并獲取結(jié)果
int result1 = f1.get();
std::cout << "Async task has been finished with result " << result1 << std::endl;
// 等待線程完成并退出
t.join();
std::cout << "Thread has been finished." << std::endl;
return 0;
}
更直觀的看一下std::launch::async 與 ,std::launch::deferred
#include <iostream>
#include <future>
int main() {
auto f1 = std::async(std::launch::deferred, []() {
std::cout << "This is a deferred async task." << std::endl;
return 1;
});
auto f2 = std::async(std::launch::async, []() {
std::cout << "This is a async task." << std::endl;
return 2;
});
std::cout << "Waiting..." << std::endl;
std::cout << f1.get() << std::endl;
std::cout << f2.get() << std::endl;
return 0;
}std::async不確定問(wèn)題的解決
不加額外參數(shù)的std::async調(diào)用問(wèn)題,讓系統(tǒng)自行決定是否創(chuàng)建新的線程。
問(wèn)題的焦點(diǎn)在于 std::future<int> result = std::async(mythread)寫法,這個(gè)異步任務(wù)到底 有沒(méi)有被推遲執(zhí)行。
實(shí)例代碼如下:
#include<iostream>
#include<thread>
#include<string>
#include<vector>
#include<list>
#include<mutex>
#include<future>
using namespace std;
int mythread() //線程入口函數(shù)
{
cout << "mythread start" << "threadid= " << std::this_thread::get_id() << endl; //打印線程id
std::chrono::milliseconds dura(5000); //定一個(gè)5秒的時(shí)間
std::this_thread::sleep_for(dura); //休息一定時(shí)常
cout << "mythread end" << "threadid= " << std::this_thread::get_id() << endl; //打印線程id
return 5;
}
int main()
{
cout << "main" << "threadid= " << std::this_thread::get_id() << endl;
std::future<int> result = std::async(mythread);//流程并不卡在這里
cout << "continue....." << endl;
//枚舉類型
std::future_status status = result.wait_for(std::chrono::seconds(0));//等待一秒
if (status == std::future_status::deferred)
{
//線程被延遲執(zhí)行了,系統(tǒng)資源緊張
cout << result.get() << endl; //此時(shí)采取調(diào)用mythread()
}
else if (status == std::future_status::timeout)//
{
//超時(shí):表示線程還沒(méi)執(zhí)行完;我想等待你1秒,希望你返回,你沒(méi)有返回,那么 status = timeout
//線程還沒(méi)執(zhí)行完
cout << "超時(shí):表示線程還沒(méi)執(zhí)行完!" << endl;
}
else if (status == std::future_status::ready)
{
//表示線程成功返回
cout << "線程成功執(zhí)行完畢,返回!" << endl;
cout << result.get() << endl;
}
cout << "I love China!" << endl;
return 0;
}到此這篇關(guān)于c++11之std::async 和std::thread的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)c++11 std::async 和std::thread內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CRC校驗(yàn)原理及其C語(yǔ)言實(shí)現(xiàn)詳解
循環(huán)冗余校驗(yàn)(Cyclic?Redundancy?Check,?CRC)是一種根據(jù)網(wǎng)絡(luò)數(shù)據(jù)包或計(jì)算機(jī)文件等數(shù)據(jù)產(chǎn)生簡(jiǎn)短固定位數(shù)校驗(yàn)碼的一種信道編碼技術(shù)。本文主要介紹了CRC校驗(yàn)原理及其C語(yǔ)言實(shí)現(xiàn),感興趣的可以了解一下2023-03-03
《C++ Primer》隱式類類型轉(zhuǎn)換學(xué)習(xí)整理
在本篇文章里小編給大家整理的是關(guān)于《C++ Primer》隱式類類型轉(zhuǎn)換學(xué)習(xí)筆記內(nèi)容,需要的朋友們參考下。2020-02-02
C/C++浮點(diǎn)數(shù)使用的兩個(gè)注意事項(xiàng)詳解
浮點(diǎn)數(shù)都是有符號(hào)的,沒(méi)有 unsigned 浮點(diǎn)數(shù),下面這篇文章主要給大家介紹了關(guān)于C/C++浮點(diǎn)數(shù)使用的兩個(gè)注意事項(xiàng),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
C++實(shí)現(xiàn)LeetCode(125.驗(yàn)證回文字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(驗(yàn)證回文字符串).本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++ normal_distribution高斯正態(tài)分布函數(shù)的用法示例
高斯分布也稱為正態(tài)分布(normal distribution),常用的成熟的生成高斯分布隨機(jī)數(shù)序列的方法由Marsaglia和Bray在1964年提出,這篇文章主要給大家介紹了關(guān)于C++ normal_distribution高斯正態(tài)分布函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2021-07-07
c語(yǔ)言float類型小數(shù)點(diǎn)后位數(shù)
在本篇文章里小編給大家整理了關(guān)于c語(yǔ)言float類型小數(shù)點(diǎn)后面有幾位的相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2020-02-02
C語(yǔ)言軟件spi虛擬總線中間層設(shè)計(jì)詳解
這篇文章主要為大家介紹了C語(yǔ)言軟件spi虛擬總線中間層設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
平衡二叉樹(shù)的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了平衡二叉樹(shù)的實(shí)現(xiàn)實(shí)例,需要的朋友可以參考下2014-02-02

