c++11之std::async 和std::thread的區(qū)別小結(jié)
std::async和std::thread都是C++11中提供的線程庫(kù),它們都可以用于創(chuàng)建新線程。它們的主要區(qū)別在于:
- std::async有時(shí)候并不創(chuàng)建新線程,而是使用線程池中的線程來執(zhí)行任務(wù),這取決于實(shí)現(xiàn)。而std::thread總是創(chuàng)建新線程。
- std::async返回一個(gè)std::future對(duì)象,可以用來獲取異步任務(wù)的結(jié)果。而std::thread沒有返回值,需要通過其他方式來獲取線程執(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沒有這樣的選項(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不確定問題的解決
不加額外參數(shù)的std::async調(diào)用問題,讓系統(tǒng)自行決定是否創(chuàng)建新的線程。
問題的焦點(diǎn)在于 std::future<int> result = std::async(mythread)寫法,這個(gè)異步任務(wù)到底 有沒有被推遲執(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í):表示線程還沒執(zhí)行完;我想等待你1秒,希望你返回,你沒有返回,那么 status = timeout //線程還沒執(zhí)行完 cout << "超時(shí):表示線程還沒執(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)文章
Qt?10進(jìn)制和16進(jìn)制轉(zhuǎn)換的使用示例
在編程過程中,處理16進(jìn)制字符串與10進(jìn)制數(shù)字之間的轉(zhuǎn)換是很常見的需求,本文主要介紹了Qt?10進(jìn)制和16進(jìn)制轉(zhuǎn)換的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Qt實(shí)現(xiàn)指針式時(shí)鐘 Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)指針式時(shí)鐘,Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘,兩者相互切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07使用C語(yǔ)言求二叉樹結(jié)點(diǎn)的最低公共祖先的方法
這篇文章主要介紹了使用C語(yǔ)言求二叉樹結(jié)點(diǎn)的最低公共祖先的方法,文中還給出了ACM的練習(xí)題目,需要的朋友可以參考下2015-08-08C語(yǔ)言進(jìn)階二叉樹的基礎(chǔ)與銷毀及層序遍歷詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)我們對(duì)C++的二叉樹基礎(chǔ)oj與二叉樹銷毀和層序遍歷進(jìn)行練習(xí),讓我們相互學(xué)習(xí),共同進(jìn)步2022-06-06c語(yǔ)言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡
這篇文章主要介紹了c語(yǔ)言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡,需要的朋友可以參考下2023-03-03sqlserver,sqlite,access數(shù)據(jù)庫(kù)鏈接字符串整理
本節(jié)主要整理sqlserver,sqlite,access數(shù)據(jù)庫(kù)鏈接字符串,有需要的朋友可以參考下2014-07-07