
Jason Hise <chaos@ezequal.com> writes:
Anthony Williams wrote:
Calls through call_once are only atomic with respect to other calls that reference the same once_flag. If you want mutual exclusion, you need a shared mutex --- why not just create a function-local static mutex, lock it before your function call, and unlock it afterwards?
Anthony
Because static variables are not initialized in a thread safe manner (except on some versions of gcc which lock the operation). Multiple threads could end up calling the mutex's constructor simultaneously, resulting in a corrupted mutex object. If I have a global mutex, I need to find a way to initialize it before the program starts spawning threads.
You can use call_once to initialize the mutex, or you can use a mutex type that supports static initialization (unlike the current boost ones, but like a pthread_mutex) Anthony -- Anthony Williams Software Developer Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk