1. 头文件
#include <future>
2. std::async()函数原型
template<typename _Fn, typename... _Args>
future<__async_result_of<_Fn, _Args...>> async(launch __policy, _Fn&& __fn, _Args&&... __args);
template<typename _Fn, typename... _Args>
future<__async_result_of<_Fn, _Args...>> async(_Fn&& __fn, _Args&&... __args);
3.功能说明
函数模板 async 异步地运行函数 __fn ,并返回最终将保有该函数调用结果的 std::future 。
4. 参数
__policy
类型为launch 的位掩码值,指示启动策略:
policy | description | launch::async | 异步:启动一个新线程来调用__fn | launch::deferred | 同步执行,还在原来的线程中,对__fn的调用被延迟,直到future使用wait()或get()。不调用这future get()或wait()将不阻塞程序。 | launch::async|launch::deferred | 自动选择,取决于系统和库的实现。如果第一个参数不填默认这个选项。 |
__fn
函数指针。
__args
函数参数,没有则不填。如果__fn是一个成员指针,则第一个参数应为定义该成员的对象(或引用,或指向该成员的指针)。
5. 函数返回值
可以使用future的get、wait、wait_for、wait_until等待执行结束,其中get可以获得执行的结果。如果选择launch::async异步执行,调用get时,如果异步执行没有结束,get会阻塞当前调用线程,直到异步执行结束并获得结果,如果异步执行已经结束,直接获得结果;如果选择launch::deferred同步执行,只有当调用get函数时,同步调用的函数才真正执行,否则不执行。
示例1, 异步调用
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
using namespace std;
std::string TestMethod1(std::string recvData)
{
std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
std::this_thread::sleep_for(chrono::seconds(5)); //等待5秒
return "Param_" + recvData;
}
int main()
{
std::cout <<"test start, main thread id is "<<std::this_thread::get_id() << std::endl;
//记录程序开始时间
chrono::system_clock::time_point start = chrono::system_clock::now();
std::cout<<"async run method"<<std::endl;
std::future<std::string> methodResult = std::async(std::launch::async, TestMethod1, "test1");
std::string resultData = methodResult.get();
std::cout<<resultData<<std::endl;
//记录程序结束时间
auto end = chrono::system_clock::now();
std::cout<<chrono::duration_cast<std::chrono::seconds>(start.time_since_epoch()).count()<<std::endl;
std::cout<<chrono::duration_cast<std::chrono::seconds>(end.time_since_epoch()).count()<<std::endl;
//得到程序执行时间并输出
auto diff = chrono::duration_cast<std::chrono::seconds>(end - start).count();
std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
return 0;
}
执行结果:
test start, main thread id is 140118678771520
async run method
TestMethod1 thread id is 140118678767360
Param_test1
1641554563
1641554568
Total time token 5 seconds
可以看出函数和主程序不在同一个线程,程序执行时间5秒,get阻塞函数直至函数执行结束(只看结果不直观,一边运行一边观察能看出来)。
示例2,同步调用
std::string TestMethod2(std::string recvData)
{
std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
std::this_thread::sleep_for(chrono::seconds(5));
return "Param_" + recvData;
}
std::string TestMethod3(std::string recvData)
{
std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
std::this_thread::sleep_for(chrono::seconds(8));
return "Param_" + recvData;
}
std::cout <<"test start, main thread id is "<<std::this_thread::get_id() << std::endl;
chrono::system_clock::time_point start = chrono::system_clock::now();
std::cout<<"async run method"<<std::endl;
std::future<std::string> methodResult = std::async(std::launch::deferred, TestMethod2, "test2");
//调用get会执行函数
std::string resultData = methodResult.get();
std::cout<<resultData<<std::endl;
std::future<std::string> methodResult2 = std::async(std::launch::deferred, TestMethod3, "test3"); //没有调用get函数,TestMethod3将不会执行
auto end = chrono::system_clock::now();
cout<<chrono::duration_cast<std::chrono::seconds>(start.time_since_epoch()).count()<<endl;
cout<<chrono::duration_cast<std::chrono::seconds>(end.time_since_epoch()).count()<<endl;
auto diff = chrono::duration_cast<std::chrono::seconds>(end - start).count();
std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
return 0;
}
执行结果:
test start, main thread id is 139882012485440
async run method
TestMethod2 thread id is 139882012485440
Param_test2
1641554955
1641554960
Total time token 5 seconds
可以看出TestMethod3没有执行,因为没调用get。同步执行函数和主程序在同一个线程。
示例3,判断返回状态
std::string TestMethod4(std::string recvData)
{
std::this_thread::sleep_for(chrono::seconds(5));
return "Param_" + recvData;
}
int main()
{
chrono::system_clock::time_point startTest = chrono::system_clock::now();
std::future<std::string> resultFromParam = std::async(std::launch::async, TestMethod4, "data");
std::future_status status;
std::string paramData;
do {
//每隔1秒读取一次状态
status = resultFromParam.wait_for(std::chrono::seconds(1));
switch(status){
case std::future_status::ready: //数据准备好了
std::cout<<"Ready..."<<std::endl;
paramData = resultFromParam.get();
std::cout<<paramData<<std::endl;
break;
case std::future_status::timeout: //等待超时
std::cout<<"waiting timeout, async function is running ..."<<std::endl;
break;
case std::future_status::deferred: //没有开始
std::cout<<"defered..."<<std::endl;
break;
default:
break;
}
}while(status != std::future_status::ready);
auto endTest = chrono::system_clock::now();
auto diff = chrono::duration_cast<std::chrono::seconds>(endTest - startTest).count();
std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
return 0;
}
执行结果:
waiting timeout, async function is running ...
waiting timeout, async function is running ...
waiting timeout, async function is running ...
waiting timeout, async function is running ...
Ready...
Param_data
Total time token 5 seconds
根据执行结果,每隔一秒读取一次状态,前几次由于程序还在执行,等到1秒超时。等函数执行完成,状态就变成了Ready。
示例4 传递类成员函数,多参数与无参数
#include <functional>
class TestAsync
{
public:
TestAsync(){
m_value = 10;
}
int TestMethod5(){
std::this_thread::sleep_for(chrono::seconds(3));
m_value++;
return m_value;
}
int TestMethod6(int value1, int value2){
std::this_thread::sleep_for(chrono::seconds(3));
m_value += value1+value2;
return m_value;
}
private:
int m_value;
};
int main()
{
chrono::system_clock::time_point startTest = chrono::system_clock::now();
TestAsync ta;
//利用std::bind绑定类的成员函数
auto func = std::bind(&TestAsync::TestMethod5, ta);
std::future<int> result = std::async(std::launch::async, func);
std::cout<<result.get()<<std::endl;
auto func2 = std::bind(&TestAsync::TestMethod6, ta, std::placeholders::_1, std::placeholders::_2);
std::future<int> result2 = std::async(std::launch::async, func2, 10, 20);
std::cout<<result2.get()<<std::endl;
auto endTest = chrono::system_clock::now();
auto diff = chrono::duration_cast<std::chrono::seconds>(endTest - startTest).count();
std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
return 0;
}
执行结果:
11
40
Total time token 6 seconds
|