2

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

How can I create a thread-safe singleton pattern in Windows?

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

Community
  • 1
  • 1
Akshay
  • 11,803
  • 5
  • 29
  • 26

2 Answers2

2

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
  • 167,383
  • 100
  • 513
  • 979
0

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

zvrba
  • 24,186
  • 3
  • 55
  • 65
  • 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 Jun 02 '10 at 12:13
  • If you have pthread, you don't need windows critical sections – basin Sep 30 '13 at 12:11