有没有办法在已经设置的情况下设置时捕获从 boost::p romise 引发的异常?



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

这里有一个最小的例子:

#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> >'

在这里你可以看到它的作用。

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

最新更新