views:

199

answers:

2

I'm trying to do the same thing as suggested in this solution:

http://stackoverflow.com/questions/164496/how-can-i-create-a-thread-safe-singleton-pattern-in-windows/164640#164640

But, where should the critical section be initialized and uninitialized?

+1  A: 

Wrap the critical section into a class (use a ready one or craft your own) and declare a global variable of that class - then the critical section will be initialized during the program startup and deinitialized on program exit. Since startup and exit are done on one thread it will work reliably.

sharptooth
Understood what you're saying. Thanks!
Akshay
+1  A: 

Use pthread_once() and you can initialize the critical section before you use it for the first time. Windows has InitOnceExecuteOnce function.

zvrba
With boost (instead of pthread_once() ), you can use a solution like I did in this one:http://stackoverflow.com/questions/2955921/thread-safe-initialization-of-function-local-static-const-objects/2956314#2956314
Nikko