【问题标题】:Is there a way to catch the exception thrown from boost::promise when setting it while it's already set?有没有办法在 boost::promise 已经设置时捕获从 boost::promise 抛出的异常?
【发布时间】:2019-05-17 05:17:19
【问题描述】:

我有一个程序,它使用计时器在 GUI 应用程序中设置一些双缓冲区。在一些罕见的情况下,例如,当程序关闭时,我收到一个错误,即设置此缓冲区的承诺已设置。有没有办法捕捉到这个错误并处理它?

这是一个最小的例子:

#include <iostream>
#include <boost/thread/future.hpp>

int main()
{
    boost::promise<int> promise;
    try {
        promise.set_value(0);
        promise.set_value(0);
    } catch (...) {
        promise.set_exception(boost::current_exception());
    }
    return 0;
}

无论我如何尝试捕捉它,它都会以错误终止我的程序:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::promise_already_satisfied> >'

here 你可以看到它的实际效果。

【问题讨论】:

    标签: c++ c++11 promise future boost-thread


    【解决方案1】:

    set_value 另一个set_value 失败后,我打赌你的catch 块中的set_exception 失败,原因相同:结果(值或异常)已经设置,承诺已经满足。至少std::promise 是这样工作的,如果boost::promise 工作相同,我不会感到惊讶。

    【讨论】:

    • 感谢您的提示。用自己的 try/catch 包装承诺集解决了这个问题。
    • @TheQuantumPhysicist 哪一组?捕获块中的set_exception?这实际上应该解决它。
    • set_value(),触发了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多