c++ - 为承诺设置例外会导致中止调用
问题描述
我有一个投掷功能:
void calculateValuesThrowing(std::promise<int>&& pr, int a, int b)
{
try
{
auto timeout = std::chrono::seconds(5);
logInfo("Calculating value...");
std::this_thread::sleep_for(timeout);
if (a == b)
{
throw std::runtime_error("a cannot equal b");
}
pr.set_value(a + b);
}
catch(std::exception& exc)
{
pr.set_exception(std::current_exception()); // ok, jump here right after the "throw" above
}
}
我这样称呼它:
main() 函数中的某处:
try
{
std::promise<int> promise;
auto future = promise.get_future();
std::thread th(calculateValuesThrowing, std::move(promise), 10, 10);
auto result = future.get();
th.join();
}
catch(std::exception& exc)
{
std::cout << "Error:" << exc.what(); // never get there
}
我希望calculateValuesThrowing的异常将被“重新抛出”,以便我可以在 main 的 catch() 中处理它,但是在calculateValuesThrowing完成工作后我得到abort()。
我究竟做错了什么?
解决方案
当future.get()
抛出时,th.join()
永远不会被调用,因此th
在线程仍处于活动状态时超出范围。这将终止程序。
试试[原文如此]这样的东西:
std::promise<int> promise;
auto future = promise.get_future();
std::thread th(calculateValuesThrowing, std::move(promise), 10, 10);
try
{
auto result = future.get();
}
catch(std::exception& exc)
{
std::cout << "Error:" << exc.what(); // never get there
}
th.join();
当然,现在你不能result
在 之外使用try
,但无论如何都是这样。我没有足够的信息来说明您如何使用它来建议具体的解决方法。
推荐阅读
- java - 当我使用 Hazelcast Jet 时,Java 堆大小会永久增长
- regex - 如何保留与 Perl 匹配的最后一个连续行
- javascript - 如何使用 Jest 来测试使用 crypto 或 window.msCrypto 的函数
- html - 包含纵向和横向图像混合的正方形离子网格
- angular - Angular 表单 formArrayName 指令用法
- javascript - Redux-form:props 中未定义形式
- java - 如何在 Spring Boot 上返回 JSONObject?
- soap - node-soap 将命名空间添加到信封
- c++ - 当我尝试在 C++ 中使用 SDL_RenderCopy 复制纹理时出现错误
- facebook - React Native Facebook SDK ShareDialog 要求再次登录