代码之家  ›  专栏  ›  技术社区  ›  The Quantum Physicist

当设置了boost::promise时,是否有方法捕获它抛出的异常?

  •  1
  • The Quantum Physicist  · 技术社区  · 6 年前

    我有一个程序,它使用计时器在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 你可以看到它在起作用。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Werner Henze    6 年前

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