相关疑难解决方法(0)

异常传播和std :: future

我的理解是,当异步操作抛出异常时,它将传播回调用的线程std::future::get().但是,当这样的线程调用时std::future::wait(),异常不会立即传播 - 它将在后续调用时被抛出std::future::get().

但是,在这种情况下,如果未来的对象在调用之后std::future::wait()但在调用之前超出范围,应该发生什么std::future::get()

对于那些感兴趣的人,这是一个简单的例子.在这种情况下,线程/ future包将以静默方式处理异常:

#include "stdafx.h"
#include <thread>
#include <future>
#include <iostream>

int32_t DoWork( int32_t i )
{
    std::cout << "i ==  " << i << std::endl;
    throw std::runtime_error( "DoWork test exception" );
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    auto f = std::async( DoWork, 5 );
    try
    {
        //f.get();     // 1 - Exception does propagate.
        f.wait();      // 2 - Exception does NOT propagate.
    }
    catch( …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading future c++11 visual-studio-2012

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

future ×1

multithreading ×1

visual-studio-2012 ×1