c++11_14学习之future async promise

1. future

c++库使用future等待一次性事件,并且有两类,一类是仅有一个std::future实例指向其关联的事件;另一类是有多个std::shared_futrue实例指向同一事件(多线程并发使用shared_futrue指向同一事件)
future用于异步任务获取值,一般与async和promise一起使用

2. async

async用于启动一个异步任务,并返回一个std::future对象,future持有最终的返回值,可通过future的get()方法阻塞等待获取。

#include <iostream>
#include <future>

struct Y
{
    double operator()(double in){
        return in;
    }
};
Y y;

int factorial(int N) {
    int res = 1;
    for (int i = N; i > 1; i--)
        res *= i;
    std::cout << "Result is: " << res << std::endl;
    return res;
}

struct X
{
    void foo(int, std::string const &)
    {

    }
    std::string bar(std::string const & in)
    {
        return in;
    }
};

X x;

int main() {
    std::future<double> f = std::async(Y(), 3.141);
    std::future<double> f1 = std::async(std::ref(y), 3.141);
    std::future<double> f2 = std::async(std::launch::async, std::ref(y), 3.141);
    std::future<double> f3 = std::async(std::launch::deferred, std::ref(y), 3.141);
    auto f4 = std::async(&X::foo, &x, 43, "hello");
    auto f5 = std::async(&X::bar, x, "goodbye");
    std::future<int> f6 = std::async(factorial, 4);

    std::cout<< f.get()<<std::endl;
    std::cout<< f1.get()<<std::endl;
    std::cout<< f2.get()<<std::endl;
    std::cout<< f3.get()<<std::endl;

    std::cout<< f6.get()<<std::endl;

    return 0;
}

std::async允许通过将额外的参数添加到调用中,来将附加参数传递给函数,这与std::thread是同样的方式。

  • 第一个参数指向成员函数指针,第二个参数则提供了用来应用该成员函数的对象(对象,指针,或者通过std::ref封装),其余的参数作为参数传递给该成员函数
  • 第二个参数作为参数传递给第一个参数所指定的函数或可调用对象
  • 如果参数是右值,则通过移动原来的参数来创建副本。

async有两种执行方式,通过第一个参数传入

  • std::launch::async 立即执行
  • std::launch::deferred 延迟执行,直到在future上调用wait()或者get()
  • std::launch::deferred | std::launch::async 也是默认方式,创建对象时是否创建新线程,这取决于implementation

3. promise

这个对象能够将信息从父线程传到子线程

  • 父线程首先创建一个promise对象,表示后续会使用它传递信息给子线程。

  • 之后使用promise对象创建future对象,作为实际的父子线程间传递信息的桥梁。

  • 然后使用async函数创建新的future对象,被调用函数需要使用promise创建的对象引用作为参数。

  • 被调用函数内使用future.get()方法,会在此等待父线程的信息。

  • 父线程后续执行完其他任务后,会使用promise.set_value(val)方法,将参数传给future,也就传给了子线程。

  • 子线程收到参数后,继续执行,最后返回值给父线程。

int factorial(std::future<int>& f) {
	int res = 1;
	
	int N = f.get();

	for (int i = N; i > 1; i--)
		res *= i;
	cout << "Result is: " << res << endl;
	return res;
}

int main() {
	int x;
	std::promise<int> p;
	std::future<int> f = p.get_future();

	std::future<int> fu = std::async(std::launch::async, factorial, std::ref(f));
	
	// do something else
	// ...

	p.set_value(4);	

	x = fu.get();
	cout << "Get from child: " << x << endl;
	return 0;
}

promise处了可以调用set_value外还可以调用set_exception,并将值保存在future中返回,常用用于异常处理返回值。

try{
	some_promise.set_value(alculate_value());
}
catch(...)
{
	some_promise.set_exception(std::current_exception())
}

注意,promise和future都只能被move,不能被copy。

4. shared_future

shared_future能够被copy,因此不用必须传引用。这样,就可以使用一个shared_future对象,供多个线程使用。

int factorial2(std::shared_future<int> f) {
    int res = 1;
    int N = f.get();
    for (int i = N; i > 1; i--)
        res *= i;
    cout << "Result is: " << res << endl;
    return res;
}

int main() {
    int x;

    std::promise<int> p;
    std::future<int> f = p.get_future();
    std::shared_future<int> sf = f.share();

    std::future<int> fu1 = std::async(factorial2, sf);
    std::future<int> fu2 = std::async(factorial2, sf);
    std::future<int> fu3 = std::async(factorial2, sf);
    // ... 10 threads

    p.set_value(4);
    cout<< fu1.get() + fu2.get() + fu3.get() << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值