Re: Thread Safety in C++ code
SQL_Learner wrote:
Hi,
I was going through this article
http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx
This talks about "C++ scoped static initialization is not thread-safe, on
purpose!"
One of the resolutions is to use to critical section to avoid race condition.
My question is what if in any program, critical section itself is static,
then there will be thread safety issue, any pointers/ideas how avoid this
condition?
You need the Win32 equivalent of pthread_once. That goes something like
this:
typedef LONG once_flag;
LONG const ONCE_FLAG_INIT = 0;
template <class T>
void once(void (*f)(T t), T t, once_flag& f)
{
if (InterlockedCompareExchange(&f, 1, 1) == 0)
{
std::owstringstream oss;
oss << "once_init_mutex_" << GetProcessId() << &f;
HANDLE mutex = CreateMutexW(NULL, TRUE, oss.str().c_str());
if (!mutex)
throw std::runtime_error("whatever");
if (GetLastError() == ERROR_ALREADY_EXISTS)
WaitForSingleObject(mutex, INFINITE);
if (f == 0)
{
try
{
f(t);
}
catch(...)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
throw;
}
f = 1;
}
ReleaseMutex(mutex);
CloseHandle(mutex);
}
}
(could add an overload for void (*f)(), etc.)
Now, say you have this function:
void f()
{
static int i = g();
//blah
}
you can make that threadsafe like this:
void initI(int& i)
{
i = g();
}
void myfunc()
{
static once_flag once = ONCE_FLAG_INIT;
static int i;
once(initI, i, once);
//blah
}
Tom