Consider the following scenario. We have a C++ function with a static local variable:
void function()
{
static int variable = obtain();
//blahblablah
To avoid the locking in any case, you can go with this:
void functionThreadSafe()
{
static int *variable = 0;
if (variable == 0)
{
CriticalSectionLockClass lock( criticalSection );
// Double check for initialization in different thread
if (variable == 0)
{
variable = new int(obtain());
}
}
//blahblablah
}