当前位置:   article > 正文

std async()详解_std::async

std::async

1. 头文件

#include <future>

2. std::async()函数原型

  1. template<typename _Fn, typename... _Args>
  2. future<__async_result_of<_Fn, _Args...>> async(launch __policy, _Fn&& __fn, _Args&&... __args);
  3. template<typename _Fn, typename... _Args>
  4. future<__async_result_of<_Fn, _Args...>> async(_Fn&& __fn, _Args&&... __args);

3.功能说明

函数模板 async 异步地运行函数 __fn ,并返回最终将保有该函数调用结果的 std::future 。

4. 参数

__policy

类型为launch 的位掩码值,指示启动策略:

policydescription
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, 异步调用

  1. #include <iostream>
  2. #include <string>
  3. #include <chrono>
  4. #include <thread>
  5. #include <future>
  6. using namespace std;
  7. std::string TestMethod1(std::string recvData)
  8. {
  9. std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
  10. std::this_thread::sleep_for(chrono::seconds(5)); //等待5秒
  11. return "Param_" + recvData;
  12. }
  13. int main()
  14. {
  15. std::cout <<"test start, main thread id is "<<std::this_thread::get_id() << std::endl;
  16. //记录程序开始时间
  17. chrono::system_clock::time_point start = chrono::system_clock::now();
  18. std::cout<<"async run method"<<std::endl;
  19. std::future<std::string> methodResult = std::async(std::launch::async, TestMethod1, "test1");
  20. std::string resultData = methodResult.get();
  21. std::cout<<resultData<<std::endl;
  22. //记录程序结束时间
  23. auto end = chrono::system_clock::now();
  24. std::cout<<chrono::duration_cast<std::chrono::seconds>(start.time_since_epoch()).count()<<std::endl;
  25. std::cout<<chrono::duration_cast<std::chrono::seconds>(end.time_since_epoch()).count()<<std::endl;
  26. //得到程序执行时间并输出
  27. auto diff = chrono::duration_cast<std::chrono::seconds>(end - start).count();
  28. std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
  29. return 0;
  30. }

执行结果:

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阻塞函数直至函数执行结束(只看结果不直观,一边运行一边观察能看出来)。如果不调用get,则函数线程和主线程并行执行,主线程继续执行,不等待函数结束。

示例2,同步调用

  1. std::string TestMethod2(std::string recvData)
  2. {
  3. std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
  4. std::this_thread::sleep_for(chrono::seconds(5));
  5. return "Param_" + recvData;
  6. }
  7. std::string TestMethod3(std::string recvData)
  8. {
  9. std::cout<<__FUNCTION__<<" thread id is "<<std::this_thread::get_id()<<std::endl;
  10. std::this_thread::sleep_for(chrono::seconds(8));
  11. return "Param_" + recvData;
  12. }
  13. int main()
  14. {
  15. std::cout <<"test start, main thread id is "<<std::this_thread::get_id() << std::endl;
  16. chrono::system_clock::time_point start = chrono::system_clock::now();
  17. std::cout<<"async run method"<<std::endl;
  18. std::future<std::string> methodResult = std::async(std::launch::deferred, TestMethod2, "test2");
  19. //调用get会执行函数
  20. std::string resultData = methodResult.get();
  21. std::cout<<resultData<<std::endl;
  22. std::future<std::string> methodResult2 = std::async(std::launch::deferred, TestMethod3, "test3"); //没有调用get函数,TestMethod3将不会执行
  23. auto end = chrono::system_clock::now();
  24. cout<<chrono::duration_cast<std::chrono::seconds>(start.time_since_epoch()).count()<<endl;
  25. cout<<chrono::duration_cast<std::chrono::seconds>(end.time_since_epoch()).count()<<endl;
  26. auto diff = chrono::duration_cast<std::chrono::seconds>(end - start).count();
  27. std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
  28. return 0;
  29. }

执行结果:

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,判断返回状态

  1. std::string TestMethod4(std::string recvData)
  2. {
  3. std::this_thread::sleep_for(chrono::seconds(5));
  4. return "Param_" + recvData;
  5. }
  6. int main()
  7. {
  8. chrono::system_clock::time_point startTest = chrono::system_clock::now();
  9. std::future<std::string> resultFromParam = std::async(std::launch::async, TestMethod4, "data");
  10. std::future_status status;
  11. std::string paramData;
  12. do {
  13. //每隔1秒读取一次状态
  14. status = resultFromParam.wait_for(std::chrono::seconds(1));
  15. switch(status){
  16. case std::future_status::ready: //数据准备好了
  17. std::cout<<"Ready..."<<std::endl;
  18. paramData = resultFromParam.get();
  19. std::cout<<paramData<<std::endl;
  20. break;
  21. case std::future_status::timeout: //等待超时
  22. std::cout<<"waiting timeout, async function is running ..."<<std::endl;
  23. break;
  24. case std::future_status::deferred: //没有开始
  25. std::cout<<"defered..."<<std::endl;
  26. break;
  27. default:
  28. break;
  29. }
  30. }while(status != std::future_status::ready);
  31. auto endTest = chrono::system_clock::now();
  32. auto diff = chrono::duration_cast<std::chrono::seconds>(endTest - startTest).count();
  33. std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
  34. return 0;
  35. }

执行结果:

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 传递类成员函数,多参数与无参数

  1. #include <functional>
  2. class TestAsync
  3. {
  4. public:
  5. TestAsync(){
  6. m_value = 10;
  7. }
  8. int TestMethod5(){
  9. std::this_thread::sleep_for(chrono::seconds(3));
  10. m_value++;
  11. return m_value;
  12. }
  13. int TestMethod6(int value1, int value2){
  14. std::this_thread::sleep_for(chrono::seconds(3));
  15. m_value += value1+value2;
  16. return m_value;
  17. }
  18. private:
  19. int m_value;
  20. };
  21. int main()
  22. {
  23. chrono::system_clock::time_point startTest = chrono::system_clock::now();
  24. TestAsync ta;
  25. //利用std::bind绑定类的成员函数
  26. auto func = std::bind(&TestAsync::TestMethod5, ta);
  27. std::future<int> result = std::async(std::launch::async, func);
  28. std::cout<<result.get()<<std::endl;
  29. auto func2 = std::bind(&TestAsync::TestMethod6, ta, std::placeholders::_1, std::placeholders::_2);
  30. std::future<int> result2 = std::async(std::launch::async, func2, 10, 20);
  31. std::cout<<result2.get()<<std::endl;
  32. auto endTest = chrono::system_clock::now();
  33. auto diff = chrono::duration_cast<std::chrono::seconds>(endTest - startTest).count();
  34. std::cout<<"Total time token "<<diff<<" seconds "<<std::endl;
  35. return 0;
  36. }

执行结果:

11

40

Total time token 6 seconds

示例5,传入类成员函数,判断函数执行是否超时

  1. #include <future>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <functional>
  5. using namespace std;
  6. class myClass
  7. {
  8. public:
  9. void test() {
  10. std::cout<<"test is called"<<std::endl;
  11. std::this_thread::sleep_for(std::chrono::milliseconds(4000));
  12. }
  13. };
  14. int main()
  15. {
  16. //myClass的一个实例
  17. myClass obj;
  18. //利用std::bind绑定类的成员函数
  19. auto func = std::bind(&myClass::test, obj);
  20. //构造future对象
  21. std::future<void> ret = std::async(std::launch::async, func);
  22. std::cout << "wait 3 seconds at most";
  23. std::chrono::milliseconds span(3000);
  24. std::future_status returnStatus = ret.wait_until(std::chrono::system_clock::now() + span);
  25. if (returnStatus == std::future_status::timeout)
  26. {
  27. std::cout<<"timeout"<<std::endl;
  28. }
  29. std::cout << "end call"<< std::endl;
  30. return 0;
  31. }

等待3秒,函数没有返回则超时。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/922437
推荐阅读
相关标签
  

闽ICP备14008679号