对于多线程可执行对象的返回值是何时返回,以及得到的呢?
对于需要用到线程返回值的线程要使用future类对象来实现
future对象
是一个类模板
提供访问异步对象的操作结果机制
- (通过 std::async 、 std::packaged_task 或 std::promise 创建的)异步操作能提供一个 std::future 对象给该异步操作的创建者。
- 异步操作的创建者能够使用多种方式查询、等待或从future中提取值。如果异步操作未能提供值,那么这写方法会阻塞
- 异步操作准备好发送结果给创建者时,它能通过修改链接到创建者的 std::future 的共享状态(例如 std::promise::set_value )进行。
#include<iostream>
#include<future>
//#include<thread>
using namespace std;
int returnThread()
{
cout << this_thread::get_id() << ">: 子线程正在执行" << endl;
cout << this_thread::get_id() << ">: 子线程开始睡眠" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout<< this_thread::get_id() << ">: 子线程睡眠了5秒" << endl;
return 10;
}
int main()
{
cout<< this_thread::get_id() << ">: 主线程正在执行" << endl;
future<int> res = async(returnThread);
cout << "线程返回值为>: " << res.get() << endl;
cout << this_thread::get_id() << ">: 主线程开始睡眠" << endl;
this_thread::sleep_for(chrono::seconds(2));
cout << this_thread::get_id() << ">: 主线程睡眠了2秒" << endl;
cout << "主线程结束" << endl;
return 1;
}
程序执行到future.get()方法时,会执行子线程,整体的进程进行会阻塞在子线程的进行,当子线程执行完毕后进程恢复,其他线程执行
async()
创建一个线程,并绑定,但是线程并执行,但是当线程中需要很十分耗时时,程序并不会阻塞在这里,而是继续执行别的线程。
#include<iostream>
#include<future>
//#include<thread>
using namespace std;
int returnThread()
{
cout << this_thread::get_id() << ">: 子线程正在执行" << endl;
cout << this_thread::get_id() << ">: 子线程开始睡眠" << endl;
this_thread::sleep_for(chrono::seconds(1));
cout<< this_thread::get_id() << ">: 子线程睡眠了1秒" << endl;
return 10;
}
int main()
{
cout << this_thread::get_id() << ">: 主线程正在执行" << endl;
future<int> res = async(returnThread);
cout << this_thread::get_id() << ">: 主线程开始睡眠" << endl;
this_thread::sleep_for(chrono::seconds(2));
cout << this_thread::get_id() << ">: 主线程睡眠了2秒" << endl;
cout << "线程返回值为>: " << res.get() << endl;
cout << "主线程结束" << endl;
return 1;
}
可能还有些不太明白,什么是进程的阻塞和线程的执行
launch::deferred参数
这个参数放到async函数的第一个位置,设置async函数仅仅是创建一个线程而不执行,当下面的代码中有get或者wait方法执行时,才会开始该线程的执行
对比一下上面的代码执行顺序
int main()
{
cout << this_thread::get_id() << ">: 主线程正在执行" << endl;
future<int> res = async(launch::deferred,returnThread);
cout << this_thread::get_id() << ">: 主线程开始睡眠" << endl;
this_thread::sleep_for(chrono::seconds(2));
cout << this_thread::get_id() << ">: 主线程睡眠了2秒" << endl;
cout << "线程返回值为>: " << res.get() << endl;
cout << "主线程结束" << endl;
return 1;
}
- 当使用了该参数,后面的代码执行不调用get或者wait则这个线程不会运行
launch::async参数
这就是async函数的默认参数,默认允许的情况下执行子线程
packaged_task
用于包装任何可调用对象的,使得能够异步调用它。其返回值和抛出异常存储于能够通过std::future对象的共享状态中
#include<iostream>
#include<future>
using namespace std;
int func(int x, int y)
{
return pow(x, y);
}
int main()
{
packaged_task<int(int, int)> task(func);
future<int> res = task.get_future();
thread taskThread(move(task), 2, 10);
taskThread.join();
cout << res.get() << endl;
packaged_task<int(int, int)> task2([](int x, int y) { return pow(x, y); });
future<int> res2 = task2.get_future();
thread taskThread2(move(task2), 2, 11);
taskThread2.join();
cout << res2.get() << endl;
}
promise
promise用于获得线程执过程中的值
#include<iostream>
#include<future>
using namespace std;
int func(int x, int y)
{
return pow(x, y);
}
void myFunction(promise<int>& pro, int val)
{
this_thread::sleep_for(chrono::seconds(5));
pro.set_value(++val);
cout << this_thread::get_id() << "线程正在执行 value = " << val << endl;
}
void myFunction2(future<int>& fu1)
{
int num = fu1.get();
this_thread::sleep_for(chrono::seconds(5));
cout << this_thread::get_id() << "线程正在执行 value = " << ++num << endl;
}
int main()
{
clock_t startTime = clock();
promise<int> pro;
thread t1(myFunction, ref(pro), 10);
t1.join();
future<int> f1 = pro.get_future();
thread t2(myFunction2, ref(f1));
t2.join();
clock_t endTime = clock();
cout << "程序耗时>: " << double(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
return 1;
}
shared_future对象
和上面的future对象不同的是,这个类对象可以多次调用get方法
- 为什么future的get方法不能多次调用?
- 原因就是,get方法是将future中的值移动到目标对象上,相当于调用了move函数,所以原来的future对象就编程empty了,这样就不能再次调用了
- 为什么shared_future的get方法能多次调用?
- 原因就是,它的get方法调用并不是移动,而是拷贝
int myThread(int num)
{
return num;
}
int useOfShared_future(shared_future<int> fu)
{
cout << this_thread::get_id() << ":>正在执行" << fu.get() << endl;
return fu.get();
}
int main()
{
packaged_task<int(int)> task(myThread);
thread t1(ref(task), 1);
shared_future<int> sfu(task.get_future());
packaged_task<int(shared_future<int>)> task2(useOfShared_future);
thread t2(ref(task2), sfu);
t1.join();
t2.join();
cout << "main函数中得到:>" << sfu.get() << endl;
}
上面的代码,在两个线程中都调用了future的get方法,但是没有出现错误,这就是shared_future是作用之处