开发者

InitOnceExecuteOnce exception safety

开发者 https://www.devze.com 2023-02-07 06:27 出处:网络
I have a excepti开发者_如何学Pythonon safety issue on InitOnceExecuteOnce WinAPI function. Whenever exception is thrown from callback function deadlock is occurred. Callback returns boolean flag telli

I have a excepti开发者_如何学Pythonon safety issue on InitOnceExecuteOnce WinAPI function. Whenever exception is thrown from callback function deadlock is occurred. Callback returns boolean flag telling the caller whether the data is successfully initialized, but if I return false I will not be able to rethrow exception, that's not good. I tried solved the problem this way.

try
{
    InitOnceExecuteOnce(&flag, init_once_handler, &arguments, 0);
} catch (...)
{
    InitOnceInitialize(&flag);
    throw;
}

Whenever I catch an exception from function I init the structure once again and rethrow the exception, so other threads will find the data not initialized, as the flag is in initial state. However, it sometimes deadlocks too, possibly because other thread started waiting on the synchronization object before the first one caught the exception and initialized flag once again. Is there any solution to that problem?

Thanks in advance.


You should not throw C++ exceptions through code that does not expect it, (eg. C code, or in your case, the Windows API). instead, have your callback return false on error, which will be reflected in the return from InitOnceExecuteOnce

0

精彩评论

暂无评论...
验证码 换一张
取 消