Consider the following scenario. We have a C++ function with a static local variable:
void function()
{
static int variable = obtain();
//blahblablah
}
the function needs to be called from multiple threads concurrently, so we add a critical section to avoid concurrent access to the static local:
void functionThreadSafe()
{
CriticalSectionLockClass lock( criticalSection );
static int variable = obtain();
//blahblablah
}
but will this be enough? I mean there's some magic that makes the variable being initialized no more than once. So there's some service data maintained by the runtime that indicates whether each static local has already been initialized.
Will the critical section in the above code protect that service data as well? Is any extra protection required for this scenario?