在下面的代码中,事件可能会抛出异常,并且它可能不会在 even handler 中处理,(很少见,但仍然是这种情况)
我想在执行事件时保持“lck2”解锁,因为我不想要“mtx2”的主线程 block ,原因无非是优化。
我能保证“lck2”总是在 catch block 中被释放吗?或者可能存在运行时异常,因此可能导致死锁或某些意外行为?
std::unique_lock<std::mutex>lck2(mtx2); // lock used for waiting for event.
while (_isRunning)
{
try
{
while (_isRunning)
{
// cvar2 is condition variable
cvar2.wait(lck2, [&] {return invoke; }); // wait until invoke == true
if (invoke) // if event must be invoked
{
lck2.unlock();
OnEvent(this, someproperty); // may throw exception
lck2.lock();
invoke = false; // execution completed
}
}
}
catch (...) // we need to keep this thread alive at all costs!
{
lck2.lock(); // is this safe?
invoke = false;
}
}
最佳答案
重写您的代码可能更合适,以使其他开发人员更容易处理代码。我将向您展示两个重写:
首先,(差)
while (true) { try { { std::lock_guard<std::mutex> lckx(mtx2); if(!_isRunning) break; //out of the main loop } bool should_invoke = false; { std::unique_lock<std::mutex> lck2(mtx2); cvar2.wait(lck2, [&] {return invoke; }); should_invoke = invoke; } if (should_invoke) // if event must be invoked { OnEvent(this, someproperty); // may throw exception { std::lock_guard<std:mutex> lckx(mtx2); invoke = false; // execution completed } } } catch (...) // we need to keep this thread alive at all costs! { std::lock_guard<std:mutex> lckx(mtx2); invoke = false; } }
第二,(好)
将(第一个)代码分解成更小的功能单元;我们还注意到表达式
cvar2.wait(lck2, [&]{ return invoke; })
将暂停执行并仅在唤醒时返回和invoke
是true
,那么我们可以推断我们只需要那个表达式等待。因此我们可以放弃对invoke
的多余使用。因此我们有:void do_work(){ while(is_running()){ try{ wait_for_invocation(); OnEvent(this, someproperty); // may throw exception set_invocation_state(false); catch(...){ set_invocation_state(false); } } }
定义助手的地方:
bool is_running(){ std::lock_guard<std::mutex> lckx(mtx2); return _isRunning; } void wait_for_invocation(){ std::unique_lock<std::mutex> lck2(mtx2); cvar2.wait(lck2, [&] {return invoke; }); } void set_invocation_state(bool state){ std::lock_guard<std::mutex> lckx(mtx2); invoke = state; }
关于c++ - 是否正确处理了此代码异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321792/