Hi there,
I’m experiencing issues with one of my singletons, which I assume is related to threads somehow (pardon my modest knowledge of C++ and multithreading).
This is what my function looks like:
SettingsManager& SettingsManager::getInstance()
{
static SettingsManager instance;
return instance;
}
So basically a regular singleton. Some googling says that this code since C++11 is thread-safe.
But when it is called (unlike my other singletons!) app hangs without any assertion or exception. Basic debuging shows that function _Init_thread_header located in thread_safe_statistics.cpp is looping forever waiting initialization status to change, which doesn’t happen.
This is the looping fuction:
// Control access to the initialization expression. Only one thread may leave
// this function before the variable has completed initialization, this thread
// will perform initialization. All other threads are blocked until the
// initialization completes or fails due to an exception.
extern "C" void __cdecl _Init_thread_header(int* const pOnce) noexcept
{
_Init_thread_lock();
if (*pOnce == uninitialized)
{
*pOnce = being_initialized;
}
else
{
while (*pOnce == being_initialized)
{
// Timeout can be replaced with an infinite wait when XP support is
// removed or the XP-based condition variable is sophisticated enough
// to guarantee all waiting threads will be woken when the variable is
// signalled.
_Init_thread_wait(xp_timeout);
if (*pOnce == uninitialized)
{
*pOnce = being_initialized;
_Init_thread_unlock();
return;
}
}
_Init_thread_epoch = _Init_global_epoch;
}
_Init_thread_unlock();
}
Any suggestions on how to overcome this? Really need my class to be a singleton and I don’t need any multithreading…
Thanks.