【发布时间】:2010-11-25 08:22:28
【问题描述】:
有人可以展示一个简单但完整的示例,说明如何通过修改下面的代码来使用 Boost 异常库在线程之间传输异常?
我正在实现的是一个简单的多线程委托模式。
class DelegeeThread
{
public:
void operator()()
{
while(true)
{
// Do some work
if( error )
{
// This exception must be caught by DelegatorThread
throw std::exception("An error happened!");
}
}
}
};
class DelegatorThread
{
public:
DelegatorThread() : delegeeThread(DelegeeThread()){} // launches DelegeeThread
void operator()()
{
while( true )
{
// Do some work and wait
// ? What do I put in here to catch the exception thrown by DelegeeThread ?
}
}
private:
tbb::tbb_thread delegeeThread;
};
【问题讨论】:
-
哇...发布后 10 小时,没有人给出答案?我的问题用词不当,还是这个问题很难?
-
我会记住,无论您最终实施什么,都可能不是您所期望的。当 DelegeeThread 想要在另一个线程中触发异常时,Delegator 可能正在做一些不相关的工作或者可能已经终止,因此捕获可能会延迟或根本不发生。
-
当然,你提到的这一点,我同意。
标签: c++ multithreading exception boost