std::future

1. future

相关demo详见

promise-demo
packaged_task-demo
furture-demo

1.1. future相关结构

Member functions

fundescpub/priv
(constructor)constructs the future object(public member function)
(destructor)destructs the future object(public member function)
operator=moves the future object(public member function)
sharetransfers the shared state from *this to a shared_future and returns it(public member function)

Getting the result

fundescpub/priv
getreturns the result(public member function)
validchecks if the future has a shared state(public member function)
waitwaits for the result to become available(public member function)
wait_forwaits for the result, returns if it is not available for the specified timeout duration(public member function)
wait_untilwaits for the result, returns if it is not available until specified time point has been reached(public member function)

1.2. async相关结构

简单快捷,简易版std::thread
std::async 并不总会开启新的线程来执行任务,你可以指定 std::launch::async 来强制开启新线程
如果 std::async 返回值 std::future 被存放在一个临时对象中,那么std::async会立马阻塞,因为临时对象在返回后立马被析构了。
std::async( std::launch::async, sleep, 5 ); // 临时对象被析构,阻塞 5s
auto f1 = std::async( std::launch::async, sleep, 5 );如果不調用f1.get(),则std::async可能都不执行

async( Function&& f, Args&&... args );
//policy: std::launch::async,std::launch::deferred,立刻异步执行或调用时再触发异步执行
async( std::launch policy, Function&& f, Args&&... args );
int asyncTime() {
    auto sleep = [](int s) { std::this_thread::sleep_for(std::chrono::seconds(s)); };

    auto start = now();
    std::async(std::launch::async, sleep, 2); // 临时对象被析构,阻塞 2s
    std::cout << "cost:" << sub(start, now()) << std::endl;//cost 2s
    start = now();
    std::async(std::launch::async, sleep, 2); // 临时对象被析构,阻塞 2s
    std::cout << "cost:" << sub(start, now()) << std::endl;//cost 2s
    
    start = now();
    auto f1 = std::async(std::launch::async, sleep, 2);
    std::cout << "cost:" << sub(start, now()) << std::endl;//cost 0s
    start = now();
    auto f2 = std::async(std::launch::async, sleep, 2);
    std::cout << "cost:" << sub(start, now()) << std::endl;//cost 0s
    f2.get();
    std::cout << "cost:" << sub(start, now()) << std::endl;//cost 2s
    return 0;
}

1.3. packaged_task 相关结构

配合 std::thread,可以取代 async
实际上就是std::futrue的封装函数,没啥特殊函数,参数是指向一个函数
可以通过 packaged_task对象直接调用关联函数(支持operator())。

    auto task = [](int i) { 
    std::this_thread::sleep_for(std::chrono::seconds(5)); return i+100; 
    };

    std::packaged_task< int(int) > package{ task };
    std::future<int> f = package.get_future();
    package(1);//直接调用
    std::cout << f.get() << "\n";

    std::packaged_task< int(int) > package2{ task };
    std::future<int> f = package.get_future();
    std::thread t { std::move(package2), 5 };//线程执行
    std::cout << f.get() << std::endl; // 阻塞,直到线程 t 结束
    t.join();

packaged_task

|Member functions

fundescpub/priv
(constructor)constructs the task object(public member function)
(destructor)destructs the task object(public member function)
operator=moves the task object(public member function)
validchecks if the task object has a valid function(public member function)
swapswaps two task objects(public member function)
get_futurereturns a std::future associated with the promised result(public member function)
operator()executes the function(public member function)
make_ready_at_thread_exitexecutes the function ensuring that the result is ready only once the current thread exits(public member function)
resetresets the state abandoning any stored results of previous executions(public member function)
std::swap(std::packaged_task)(C++11)specializes the std::swap algorithm(function template)
std::uses_allocatorstd::packaged_taskspecializes the std::uses_allocator type trait(class template specialization)

1.4. promise相关结构

更像一个线程间同步的封装类,一个调用set_value设置变量,一个调用get_future().get()获取变量

    auto task = [](std::future<int> i) {
        std::cout << i.get() << std::flush; // 阻塞,直到 p.set_value() 被调用
    };

    std::promise<int> p;
    std::thread t{ task, p.get_future() };

    std::this_thread::sleep_for(std::chrono::seconds(5));
    p.set_value(5);

    t.join();

promise

Member functions

fundescpub/priv
(constructor)constructs the promise object(public member function)
(destructor)destructs the promise object(public member function)
operator=assigns the shared state(public member function)
swapswaps two promise objects(public member function)

Getting the result

fundescpub/priv
get_futurereturns a future associated with the promised result(public member function)
set_valuesets the result to specific value(public member function)
set_value_at_thread_exitsets the result to specific value while delivering the notification only at thread exit(public member function)
set_exceptionsets the result to indicate an exception(public member function)
set_exception_at_thread_exitsets the result to indicate an exception while delivering the notification only at thread exit(public member function)
std::swap(std::promise)specializes the std::swap algorithm(function template)
std::uses_allocatorstd::promisespecializes the std::uses_allocator type trait(class template specialization)

1.5. 参考资料

  1. C++11并发指南系列
  2. cppreference
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值