我在我的应用程序中使用 boost 库进行线程和同步。
首先,我必须说同步线程中的异常对我来说是新事物。无论如何,下面是我想要实现的伪代码。我希望同步线程抛出与执行通知的线程可能抛出的相同异常。我怎样才能做到这一点?
无法从 Stack Overflow 中找到有关使用 boost 线程模型的跨线程交互引发异常的任何主题
提前谢谢了!
// mutex and scondition variable for the problem
mutable boost::mutex conditionMutex;
mutable boost::condition_variable condition;
inline void doTheThing() const {
if (noone doing the thing) {
try {
doIt()
// I succeeded
failed = false;
condition.notify_all();
}
catch (...) {
// I failed to do it
failed = true;
condition.notify_all();
throw
}
else {
boost::mutex::scoped_lock lock(conditionMutex);
condition.wait(lock);
if (failed) {
// throw the same exception that was thrown from
// thread doing notify_all
}
}
}