目录
1、Future
class td::future 提供处理并发运算的未来结果的能力。
2、Future的成员函数
Furture f; //default构造函数,建立一个无状态的future object
Future f(rv); //move构造函数,建立一个新的future,状态取至rv,并令rv状态失效
f.~future(); //销毁状态也销毁*this
f = rv; //Move assignment;销毁f的旧状态,取rv状态,并令rv状态失效
f.valid(); //如果f具备有效状态就获得true,然后你才可以调用下一个成员函数
f.get(); //阻塞直到后台完成操作。它会迫使被推迟的线程同步启动,产出结果或发出异常,并令其状态失效
f.wait(); //阻塞直到后台操作完成,。它会迫使被推迟的线程同步启动。
f.wait_for(dur); //阻塞dur时间段,或直到后台操作完成。但被推迟的线程不会被启动。
f.wait_until(tp); //阻塞直到时间点tp,或直到后台操作完成。但被推迟的线程不会被启动。
f.share(); //产生一个shared_future带有当前状态,并令f的状态失效
future_status s; //枚举ready(成功)、timeout(超时)、deferred(延迟)
3、async
async用来创建异步任务(线程)的。它有三种策略:
1)launch::async 立刻执行。async(std::launch::async,function);
2)launch::deferred 推迟执行 等待get,wait。async(std::launch::deferred ,function);
3)无策略,能立刻执行就马上执行,否则等待执行。async(function)。
async()的参数可以是:函数、成员函数、函数对象、lamda等。
4、测试代码
#include <future>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
#include <exception>
#include <ctime>
using namespace std;
int doSomething(char c)
{
default_random_engine dre(c);
uniform_int_distribution<int> id(10, 100);
for ( int i = 0; i < 10; ++i)
{
this_thread::sleep_for(chrono::milliseconds(id(dre)));
cout.put(c).flush();
}
return c;
}
int fun1()
{
return doSomething('-');
}
int fun2()
{
return doSomething('+');
}
int main()
{
cout << "starting fun1() itn background and fun2() in foreground: " << endl;
future<int> result1(async(launch::async,fun1));
//同上
//future<int> result3(async(launch::async, doSomething, '-'));
//auto t = async(launch::async, []{ doSomething('-'); });
//等待result1完成,并将它的结果和result2相加
int result2 = fun2();
int result = result1.get() + result2;
/*
get():的作用:
1)线程结束,马上获取结果;
2)线程未结束,等待线程结束,获取结果;
3)线程未启动,启动线程,等待获取结果。
*/
//async()的参数可以是:函数、成员函数、函数对象、lamda等。
future_status s;//ready(成功)、timeout(超时)、deferred(延迟)
result1.wait(); //等待线程完成
s = result1.wait_for(chrono::seconds(10));//等待10秒
if ( s == future_status::deferred)
{
cout << "线程延迟 " << endl;
}
cout << " wait 10s was finished." << endl;
result1.wait_until(chrono::system_clock::now() + std::chrono::minutes(1));
cout << " wait 1m was finished." << endl;
cout << " result1l + result2 = " << result << endl;
system("pause");
}
5、shared_future
处理多个线程需要同一个算法结果问题。
#include <future>
#include <thread>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int queryNumber()
{
cout << "input number: ";
int num = 0;
cin >> num;
if (!cin)
{
throw runtime_error("No inputing number.");
}
return num;
}
void doSomething(char c, shared_future<int> &f)// 参数2,引用或者传值均行
{
try {
int num = f.get();//多次访问
for (int i = 0; i < num; ++i)
{
this_thread::sleep_for(chrono::seconds(3));
cout.put(c).flush();
}
}
catch (const exception& e)
{
cerr << "EXCEPTION in thread " << this_thread::get_id()
<< ": " << e.what() << endl;
}
}
int main()
{
try {
//共享结果
//shared_future<int> f = async(queryNumber);
auto f = async(queryNumber).share();
//均需要f的返回值 启动线程
auto f1 = async(launch::async, doSomething, '.', ref(f));
auto f2 = async(launch::async, doSomething, '+', ref(f));
auto f3 = async(launch::async, doSomething, '*', ref(f));
f1.get();//等待线程结束
f2.get();
f3.get();
}
catch (const exception& e) {
cout << "\nEXCEPTION: " << e.what() << endl;
}
system("pause");
}