Buk*_*kes 11 c++ multithreading future c++11 visual-studio-2012
我的理解是,当异步操作抛出异常时,它将传播回调用的线程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( std::exception& e )
{
std::cout << e.what() << std::endl;
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GMa*_*ckG 19
它会被忽略并丢弃,就像你wait()是一个值但从来没有get()它.
wait()简单地说"阻止直到未来准备好",准备好了价值或例外.这取决于调用者实际上get()的值(或异常).通常你只是使用get(),无论如何都会等待.
| 归档时间: |
|
| 查看次数: |
11784 次 |
| 最近记录: |