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)文章
C語(yǔ)言學(xué)習(xí)之鏈表的實(shí)現(xiàn)詳解
鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的。這篇文章主要介紹了C語(yǔ)言中鏈表的實(shí)現(xiàn),需要的可以參考一下2022-11-11
Qt中定時(shí)器 QTimerEvent 和 QTimer的使用
Qt框架中的定時(shí)器功能主要包括兩種實(shí)現(xiàn)方式,包括QTimerEvent和QTimer,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01

