A: 

I would check out this article. It is a solution for C++ singletons, but I believe you can use the solution for your code as well: http://www.ddj.com/cpp/199203083?pgno=1

Sadly the listing for the QLock itself is missing, it looks as if they are trying to sell the CD, but there appears to be enough description of it to write one yourself.

grieve
+1  A: 
James Fisher
Isn't it the Double Checked Locking antipattern? It is supposed to NOT work portably. I would not use it without intensive testing on my platform/outside the platform for which it was written... See the following article by S. Meyers and A. Alexandrescu: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
paercebal
+3  A: 

I think you want to use the One-Time Initialization functionality. In synchronous mode, all threads block until the first thread to call it completes. Seems analogous to pthread_once().

There is sample code here.

So in your case, you would say:

BOOL CALLBACK CallLibInitInternal(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) {
    libInitInternal();
    return TRUE;
}

void libInit() {
#ifdef WIN32
    static INIT_ONCE s_init_once;
    InitOnceExecuteOnce(&s_init_once, CallLibInitInternal, NULL, NULL);
#else
...
#endif
}
jeffamaphone
This can be used only on Windows Vista and newer. I'd love to have something that will work on Windows 98.
Přemysl Janouch
Really? I totally missed that in the docs. Sorry. You'll need to write the code and your OP looks close. Probably want to create a named object that everyone waits on so they're all waiting on the same object.
jeffamaphone
+2  A: 

You might want to check what pthreads-win32 does in its pthread_once() implementaion. or just use that, if that proves to be easier.

Hasturkun