سؤال

I have a exception 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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top