是否正确处理了此代码异常?
Is this code exceptions handled correctly?
在下面的代码中,事件可能会引发异常,并且可能无法在处理程序中处理,(很少见,但仍然如此(
我想在执行事件时保持"lck2"解锁,因为我不想要"mtx2"的主线程块,原因无非是优化。
我可以保证"lck2"总是在catch块中释放吗? 或者可能存在运行时异常,因此可能会导致死锁或某些意外行为?
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++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- VisualStudios 会抛出异常,而代码块不会 [C++]
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 为什么代码段会引发浮点异常?
- 我应该C++自定义异常代码放在哪里?
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- C++中的奇怪异常代码,当用户输入为 23 时,退出值 1,073,740,940(代码:0xc0000374)
- 如何在Visual Studio(2015)中增加堆栈尺寸的堆栈尺寸?异常代码:C00000FD
- 从覆盖范围报告中删除自动生成的异常代码
- 防火墙异常代码仅适用于传出连接
- 需要对异常代码进行解释
- C/C++语言-指针和算术..异常代码:c0000005
- 异常代码"EXC_I386_GPFLT"的含义是什么?
- 单元测试失败,异常代码为c0000005
- 如何使异常代码DRY
- 访问计算机指针时发生访问冲突(c系统异常代码:c0000005),导致应用程序崩溃
- 如何在Windows上诊断异常代码0xc0000409
- 如何在iOS Objective-C中捕获C++异常代码