c++11之std::async 和std::thread的區(qū)別小結(jié)
std::async和std::thread都是C++11中提供的線程庫,它們都可以用于創(chuàng)建新線程。它們的主要區(qū)別在于:
- std::async有時候并不創(chuàng)建新線程,而是使用線程池中的線程來執(zhí)行任務,這取決于實現(xiàn)。而std::thread總是創(chuàng)建新線程。
- std::async返回一個std::future對象,可以用來獲取異步任務的結(jié)果。而std::thread沒有返回值,需要通過其他方式來獲取線程執(zhí)行的結(jié)果。
- std::async可以指定任務的執(zhí)行策略,例如std::launch::async表示一定要創(chuàng)建新線程執(zhí)行任務,std::launch::deferred表示可以不創(chuàng)建新線程,等到調(diào)用std::future的get()方法時再執(zhí)行任務。而std::thread沒有這樣的選項。
#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)建異步任務 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; // 等待異步任務完成并獲取結(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)建新的線程。
問題的焦點在于 std::future<int> result = std::async(mythread)寫法,這個異步任務到底 有沒有被推遲執(zhí)行。
實例代碼如下:
#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); //定一個5秒的時間 std::this_thread::sleep_for(dura); //休息一定時常 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; //此時采取調(diào)用mythread() } else if (status == std::future_status::timeout)// { //超時:表示線程還沒執(zhí)行完;我想等待你1秒,希望你返回,你沒有返回,那么 status = timeout //線程還沒執(zhí)行完 cout << "超時:表示線程還沒執(zhí)行完!" << endl; } else if (status == std::future_status::ready) { //表示線程成功返回 cout << "線程成功執(zhí)行完畢,返回!" << endl; cout << result.get() << endl; } cout << "I love China!" << endl; return 0; }
到此這篇關于c++11之std::async 和std::thread的區(qū)別小結(jié)的文章就介紹到這了,更多相關c++11 std::async 和std::thread內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Qt實現(xiàn)指針式時鐘 Qt實現(xiàn)動態(tài)時鐘
這篇文章主要為大家詳細介紹了Qt實現(xiàn)指針式時鐘,Qt實現(xiàn)動態(tài)時鐘,兩者相互切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07sqlserver,sqlite,access數(shù)據(jù)庫鏈接字符串整理
本節(jié)主要整理sqlserver,sqlite,access數(shù)據(jù)庫鏈接字符串,有需要的朋友可以參考下2014-07-07