I'm trying to do the same thing as suggested in this solution:
But, where should the critical section be initialized and uninitialized?
I'm trying to do the same thing as suggested in this solution:
But, where should the critical section be initialized and uninitialized?
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.
Use pthread_once() and you can initialize the critical section before you use it for the first time. Windows has InitOnceExecuteOnce function.