JavaMemoryModel: static singletons in C on win32

From: Doug Lea (
Date: Mon Jan 08 2001 - 10:19:08 EST

This is a little off the subject, but related to discussions of
double-check and its variants. Maybe everyone else already knew this,
but it was surprising to me.

As far as I can see (I also checked with Doug Schmidt), there is
no way to statically initialize a WIN32 CRITICAL_SECTION lock in C.

In posix, this can be done via:
   static pthread_mutex_t amutex = PTHREAD_MUTEX_INITIALIZER;

But in WIN32, you need to call InitializeCriticalSection() before you
can use a lock. This makes it exceedingly tricky to define static
singletons that need a static lock to protect construction. Trying to
use double-check for the static lock itself of course leads to
infinite regress. So I guess you'd need to roll-your-own
dekker-algorithm locks or use raw CAS or test-and-set or somesuch? I
wonder how many people get this right. Or maybe there is some

In C++, you can wrap the CRITICAL_SECTION in a class that that will
initialize it upon construction (as in ACE, and Schmidt et al's POSA2
book). But even in this case, I don't know if C++ officially requires,
and win32 runtimes actually provide, enough static initialization
safeguards for this to occur reliably?


JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:29 EDT