一、std::async、std::future 创建后台任务并返回值
希望线程返回一个结果;
std::async是一个函数模板,用来启动一个异步任务,启动起来一个异步任务之后,返回一个std::future对象。
什么叫做启动一个异步任务?就是自动创建一个线程并开始执行对应的线程入口函数,它返回一个std::future对象。这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果);我们可以调用future对象的成员函数get()来获取结果。
future 将来的意思,有人也称呼std::future 提供了一种返回异步操作结果的机制,就是这个结果不能马上得到,但是在不久的将来,在线程执行完毕的时候,就可以拿到结果。所以这么理解:这个future对象里面会保存一个值,在将来获取这个值。
下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果;get()这个成员函数有一种不拿到结果誓不罢休的感觉,哈哈哈!
#include <iostream>
#include <list>
#include <mutex>
#include <future>
using namespace std;
int mythread()
{
cout << "mythread() start," << "threadid=" << std::this_thread::get_id() << endl;
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
cout << "mythread() end," << "threadid=" << std::this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "main() start," << "threadid=" << std::this_thread::get_id() << endl;
std::future<int> result = std::async(mythread); //创建一个线程并开始执行
cout << "continue...!" << endl;
int def = 0;
cout << result.get() << endl; //卡在这里等待 mythread()执行完毕,拿到结果
cout << result.get() << endl; // 实验一下是否能够多次get(),get()只能调用1次你,不能调用多次
//result.wait();//等待线程返回,本身不返回结果
cout << "I Love China" << endl;
return 0;
}
//用成员函数做线程入口函数
#include <iostream>
#include <future>
using namespace std;
class A
{
public:
int mythread(int i)
{
cout << "int mythread() begin," << "thread id=" << std::this_thread::get_id() << endl;
cout << "int mythread(int i),i=" << i << endl;
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
cout << "int mythread() end," << "thread id=" << std::this_thread::get_id() << endl;
return 5;
}
};
int main()
{
A obj;
int a = 100;
cout << "int main() begin," << "thread id=" << std::this_thread::get_id() << endl;
std::future<int> result = std::async(&A::mythread,&obj,a);
cout << "continue...!" << endl;
cout << result.get() << endl; //如果将result.get()注释掉,程序还是会等到mythread执行结束,
//这个有点奇怪
cout << "I Love China!" << endl;
return 0;
}
我们额外的通过向std::async()传递一个参数,该参数类型是std::lunch类型(枚举类型),来达到一些特殊的目的。
a.std::launch::deferred :表示线程入口函数调用被延迟到std::future的wait() 或者 get() 函数调用时才执行。那如果wait() 或者 get() 没有被调用,那么线程会执行吗?经过实验,线程不会被执行,而且也根本不会被创建。
std::launch::deferred :延迟调用,并没有创建新线程,是在主线程中调用的线程入口函数。
b.std::launch::async,表示在调用async函数的时候就开始创建线程。
async(...)函数,第一个参数默认为std::launch::async
#include <iostream>
#include <future>
using namespace std;
class A
{
public:
int mythread(int i)
{
cout << "int mythread() begin," << "thread id=" << std::this_thread::get_id() << endl;
cout << "int mythread(int i),i=" << i << endl;
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
cout << "int mythread() end," << "thread id=" << std::this_thread::get_id() << endl;
return 5;
}
};
int main()
{
A obj;
int a = 100;
cout << "int main() begin," << "thread id=" << std::this_thread::get_id() << endl;
std::future<int> result = std::async(std::launch::async,&A::mythread,&obj,a);
//std::future<int> result = std::async(&A::mythread, &obj, a);
cout << "continue...!" << endl;
cout << result.get() << endl; //如果将result.get()注释掉,程序还是会等到mythread执行结束,
cout << "I Love China!" << endl;
return 0;
}
二、std::packaged_task:打包任务,把任务包装起来。
是个类模板,它的模板参数是各种可调用对象;通过std::packaged_task 来把各种可调用对象包装起来,方便将来作为线程入口函数来调用。
demo1:
#include <iostream>
#include <future>
using namespace std;
int mythread(int i)
{
cout << "int mythread(int i),i=" << i << endl;
cout << "int mythread() begin,thread id=" << std::this_thread::get_id() << endl;
chrono::milliseconds dura(5000);
this_thread::sleep_for(dura);
cout << "int mythread() end,thread id=" << std::this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
packaged_task<int(int)> mypt(mythread); //通过把函数mythread通过packaged_task包装起来
thread t1(std::ref(mypt),1);
t1.join();
future<int> result = mypt.get_future(); // std::future对象里面包含有线程入口函数的返回结果,这里result保存mythred线程的返回结果
cout << result.get() << endl;
cout << "I Love China!" << endl;
return 0;
}
demo2:
用lambda表达式。
#include <iostream>
#include <future>
using namespace std;
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
packaged_task<int(int)> mypt([](int mypar) {
cout << "int mythread(int mypar),mypar=" << mypar << endl;
cout << "int mythread() begin,thread id=" << std::this_thread::get_id() << endl;
chrono::milliseconds dura(5000);
this_thread::sleep_for(dura);
cout << "int mythread() end,thread id=" << std::this_thread::get_id() << endl;
return 5;
});
thread t1(ref(mypt),1);
t1.join();
future<int> result = mypt.get_future();
cout << result.get() << endl;
cout << "I Love China!" << endl;
return 0;
}
demo3:
packaged_task包装起来的可调用对象还可以直接调用,所以从这个角度来讲,packaged_task对象,也是一个可调用对象。
此时没有创建新线程,主线程和子线程一样。
#include <iostream>
#include <future>
using namespace std;
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
packaged_task<int(int)> mypt([](int mypar) {
cout << "int mythread(int mypar),mypar=" << mypar << endl;
cout << "int mythread() begin,thread id=" << std::this_thread::get_id() << endl;
chrono::milliseconds dura(5000);
this_thread::sleep_for(dura);
cout << "int mythread() end,thread id=" << std::this_thread::get_id() << endl;
return 5;
});
mypt(105); //直接调用,相当于函数调用,并没有创建新线程
future<int> result = mypt.get_future(); //此时没有创建新线程,主线程和子线程一样
cout << result.get() << endl;
cout << "I Love China!" << endl;
return 0;
}
demo4:
#include <iostream>
#include <future>
#include <vector>
using namespace std;
vector<packaged_task<int(int)>> mytasks;
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
packaged_task<int(int)> mypt([](int mypar) {
cout << "int mythread(int mypar),mypar=" << mypar << endl;
cout << "int mythread() begin,thread id=" << std::this_thread::get_id() << endl;
chrono::milliseconds dura(5000);
this_thread::sleep_for(dura);
cout << "int mythread() end,thread id=" << std::this_thread::get_id() << endl;
return 5;
});
mytasks.push_back(std::move(mypt)); //入容器,这里用了移动语义,入进去之后mypt就为空。
//...........
packaged_task<int(int)> mypt2;
auto iter = mytasks.begin();
mypt2 = move(*iter); // 移动语义
mytasks.erase(iter); //删除第一个元素,迭代器已经失效了,所以后续代码不可以再使用iter了。
mypt2(123); //直接调用,相当于函数调用,并没有创建新线程
future<int> result = mypt2.get_future();
cout << result.get() << endl;
cout << "I Love China!" << endl;
return 0;
}
三、std::promise
是一个类模板,我们能够再某个线程中给它赋值,然后再其他线程中,把这个值取出来用。
总结:通过promise 保存一个值,在将来的某个时刻,我们通过把一个future绑定到这个promise上来得到这个绑定的值。
#include <iostream>
#include <future>
#include <vector>
using namespace std;
void mythread(promise<int>& tmp, int calc)
{
cout << "mythread(promise<int>& tmp, int calc),thread id=" << std::this_thread::get_id() << endl;
calc++;
calc *= 10;
//做其他运算,比如花费整整5秒钟
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
//终于计算出结果
int result = calc;
tmp.set_value(result); //结果我保存到了tmp这个对象中
return;
}
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
promise<int> myprom;
std::thread t1(mythread,std::ref(myprom),180);
t1.join();
future<int> ful = myprom.get_future(); //promise 和 future 绑定,用于获取线程返回值
cout << ful.get() << endl;
cout << "I Love China!" << endl;
return 0;
}
demo2:
#include <iostream>
#include <future>
#include <vector>
using namespace std;
void mythread(promise<int>& tmp, int calc)
{
cout << "mythread(promise<int>& tmp, int calc),thread id=" << std::this_thread::get_id() << endl;
calc++;
calc *= 10;
//做其他运算,比如花费整整5秒钟
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
//终于计算出结果
int result = calc;
tmp.set_value(result); //结果我保存到了tmp这个对象中
return;
}
void mythread2(future<int>& tmp)
{
auto result = tmp.get();
cout << "void mythread2(future<int>& tmp) result:" << result << endl;
return;
}
int main()
{
cout << "int mythread() main,thread id=" << std::this_thread::get_id() << endl;
promise<int> myprom;
std::thread t1(mythread,std::ref(myprom),180);
t1.join();
future<int> ful = myprom.get_future(); //promise 和 future 绑定,用于获取线程返回值
//cout << ful.get() << endl;
thread t2(mythread2,ref(ful));
t2.join(); // 等待线程thread2执行完毕
cout << "I Love China!" << endl;
return 0;
}