四、future和promise
C++11創建了線程之後,我們不能直接從thread.joid()獲取結果,C++11提供了future來獲取異步操作的結果。這個結果是不能立即獲取的,會在未來的某個時間內進行獲取。而future和和promise的作用就是在線程之間傳遞數據。
#include <future>
void test_pro(promise<string> &p){
this_thread::sleep_for(chrono::milliseconds(2000));
//傳值
p.set_value("hello, this is my value");
}
void test_future(){
promise<string> pro;
thread t1(test_pro, ref(pro));
cout << "thread1 is running;" << endl;
//promise和future進行綁定
future<string> f = pro.get_future();
//從future中獲取值,在p.set_value()之後就可以獲取值了
//在p.set_value()之前,線程會一直阻塞在此處
auto res = f.get();
cout << "res : " << res << endl;
}
此處獲取異步操作結果使用的是get,當然還有wait以及wait_for。
wait_for:
函數原型:
template< class Rep, class Period >
std::future_status wait_for( const std::chrono::duration<Rep,Period>& timeout_duration ) const;
阻塞至結果變得可用,或者阻塞至經過指定的timeout_duration,返回值鑑別結果的狀態。
參數timeout_duration是阻塞的最大時長。
返回結果有三種狀態:
- future_status::deferred:要計算結果的函數未啓動
- future_status::ready:結果準備就緒
- future_status::timeout:超時
void test_wait_for(){
std::future<int> future = std::async(std::launch::async, [](){
std::this_thread::sleep_for(std::chrono::seconds(3));
return 6;
});
std::cout << "waiting...\n";
std::future_status status;
do {
cout << "check status." << endl;
status = future.wait_for(std::chrono::seconds(1));
switch (status){
case std::future_status::deferred :
std::cout << "deferred\n";
break;
case std::future_status::timeout:
std::cout << "timeout\n";
break;
case std::future_status::ready:
std::cout << "ready!\n";
break;
}
} while (status != std::future_status::ready);
std::cout << "result is " << future.get() << '\n';
}
五、future和package_task
package_task包裝了一個可調用對象,允許異步獲取該可調用對象產生的結果。package_task將其包裝的可調用對象的執行結果傳遞給一個future對象。
C++中可調用對象:函數、函數指針、lambda表達式、重載了函數調用運算符的函數對象類以及bind創建的對象。
int addOne(int x){
return x + 1;
}
void test_package_task(){
packaged_task<int(int)> pt1(addOne);
future<int> fu1 = pt1.get_future();
//聲明一個變量
int c = 0;
//創建一個線程t1,將pt1及對應的參數放到線程裏面執行
std::thread t1(std::move(pt1), c);
//阻塞至線程t1結束(函數addOne返回結果)
int iResult = fu1.get();
std::cout << "執行結果:" << iResult << std::endl; //執行結果:1
std::cout << "c:" << c << std::endl; //c:0
}
六、線程異步操作函數async
async可以創建異步任務,並且將其運行結果保存在future中,我們可以在外面從future中取值。
async原型:async(std::launch::async | std::launch::deferred, f, args...)
第一個參數是線程創建策略:
- std::launch::async:在調用async的時候就創建線程
- std::launch::deferred:延遲加載的方式創建線程。即調用async的時候不創建線程,在調用future的get或wait的時候才創建線程
第二個參數是線程要執行的函數,之後的參數是往線程執行函數中傳遞的參數。
void test_async(){
//策略1
std::future<int> fut = std::async(std::launch::async, []() {
cout<<" 開始執行async" << endl;
return 1;
});
//策略2
std::future<int> fut2 = std::async(std::launch::deferred, []() {
cout<<" 開始執行deferred" << endl;
return 2;
});
std::cout << "result:" << fut.get() << std::endl;
this_thread::sleep_for(chrono::milliseconds(2000));
std::cout << "result:" << fut2.get() << std::endl;
}