在n3721 上写下关于改进std::future
API的论文。关于未包装的期货,似乎存在异常传播竞赛。文件说
如果外部 Future 抛出异常,并且在返回的 Future 上调用 .get() ,则返回的 Future 会抛出与外部 Future 相同的异常。之所以如此,是因为内心的未来没有退出
所以我的意思是在以下情况下
#include <iostream>
#include <future>
#include <exception>
using namespace std;
int main() {
auto prom_one = std::promise<std::future<int>>{};
auto fut_one = prom_one.get_future();
std::thread{[prom_one = std::move(prom_one)]() mutable {
auto prom_two = std::promise<int>{};
auto fut_two = prom_two.get_future();
std::thread{[prom_two = std::move(prom_two)]() mutable {
prom_two.set_exception(std::make_exception_ptr(std::logic_error{}));
}}.detach();
prom_one.set_exception(std::make_exception_ptr(std::bad_alloc{}));
}}.detach();
auto inner_fut = fut_one.unwrap();
cout << inner_fut.get() << endl;
return 0;
}
我之前谈到的比赛是——会抛出哪个异常?内部std::logic_error
还是外部std::bad_alloc
?
我弄错了吗?上面的代码中没有种族吗?