async 创建异步线程
async 创建异步线程
#include <thread>
#include <iostream>
#include <future>
#include <string>
using namespace std;
string TestAsync(int index)
{
cout << index<<" begin in TestAsync " << this_thread::get_id() << endl;
this_thread::sleep_for(2s);
return "TestAsync string return";
}
int main(int argc, char* argv[])
{
//创建异步线程
cout << "main thread id " << this_thread::get_id() << endl;
//不创建线程启动异步任务
auto future = async(launch::deferred, TestAsync,100);
this_thread::sleep_for(100ms);
cout << "begin future get " << endl;
cout << "future.get() = " << future.get() << endl;
cout << "end future get" << endl;
//创建异步线程
cout << "=====创建异步线程====" << endl;
auto future2 = async(TestAsync, 101);
this_thread::sleep_for(100ms);
cout << "begin future2 get " << endl;
cout << "future2.get() = " << future2.get() << endl;
cout << "end future2 get" << endl;
getchar();
return 0;
}
原文地址:https://blog.csdn.net/qq_45159887/article/details/143026959
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!