先說明一點:std::asyanc是std::future的高級封裝, 一般我們不會直接使用std::futrue,而是使用對std::future的高級封裝std::async。 下面分別說一下。
一、std::async基本用法
std::future可以從異步任務中獲取結果,一般與std::async配合使用,std::async用於創建異步任務,實際上就是創建一個線程執行相應任務。
std::async就是異步編程的高級封裝,封裝了std::future的操作,基本上可以代替std::thread 的所有事情。
std::async的操作,其實相當於封裝了std::promise、std::packaged_task加上std::thread。
使用代碼如下:
#include <future> #include <iostream> #include <stout/stringify.hpp> bool is_prime(int x) { for (int i=0; i<x; i++) { if (x % i == 0) return false; } return true; } int main() { std::future<bool> fut = std::async(is_prime, 700020007); std::cout << "please wait"; std::chrono::milliseconds span(100); while (fut.wait_for(span) != std::future_status::ready) std::cout << "."; std::cout << std::endl; bool ret = fut.get(); std::cout << "final result: " << stringify(ret) << std::endl; return 0; }
std::async會首先創建線程執行is_prime(700020007), 任務創建之后,std::async立即返回一個std::future對象。
主線程既可使用std::future::get獲取結果,如果調用過程中,任務尚未完成,則主線程阻塞至任務完成。
主線程也可使用std::future::wait_for等待結果返回,wait_for可設置超時時間,如果在超時時間之內任務完成,則返回std::future_status::ready狀態;如果在超時時間之內任務尚未完成,則返回std::future_status::timeout狀態。
上面先說了通用的做法,然后我們了解一下std::future、std::promise、std::packaged_task
二、std::future說明
future對象是std::async、std::promise、std::packaged_task的底層對象,用來傳遞其他線程中操作的數據結果。
三、std::promise用法
std::promise的作用就是提供一個不同線程之間的數據同步機制,它可以存儲一個某種類型的值,並將其傳遞給對應的future, 即使這個future不在同一個線程中也可以安全的訪問到這個值。
示例代碼:
// promise example #include <iostream> // std::cout #include <functional> // std::ref #include <thread> // std::thread #include <future> // std::promise, std::future void print_int (std::future<int>& fut) { int x = fut.get(); std::cout << "value: " << x << '\n'; } int main () { std::promise<int> prom; // create promise std::future<int> fut = prom.get_future(); // engagement with future std::thread th1 (print_int, std::ref(fut)); // send future to new thread prom.set_value (10); // fulfill promise // (synchronizes with getting the future) th1.join(); return 0; }
Output:
value: 10
四、std::packaged_task用法
std::packaged_task的作用就是提供一個不同線程之間的數據同步機制,它可以存儲一個函數操作,並將其返回值傳遞給對應的future, 而這個future在另外一個線程中也可以安全的訪問到這個值。
示例代碼:
// packaged_task example #include <iostream> // std::cout #include <future> // std::packaged_task, std::future #include <chrono> // std::chrono::seconds #include <thread> // std::thread, std::this_thread::sleep_for // count down taking a second for each value: int countdown (int from, int to) { for (int i=from; i!=to; --i) { std::cout << i << '\n'; std::this_thread::sleep_for(std::chrono::seconds(1)); } std::cout << "Lift off!\n"; return from-to; } int main () { std::packaged_task<int(int,int)> tsk (countdown); // set up packaged_task std::future<int> ret = tsk.get_future(); // get future std::thread th (std::move(tsk),10,0); // spawn thread to count down from 10 to 0 // ... int value = ret.get(); // wait for the task to finish and get result std::cout << "The countdown lasted for " << value << " seconds.\n"; th.join(); return 0; }
Possible Output:
10 9 8 7 6 5 4 3 2 1 Lift off! The countdown lasted for 10 seconds.
