63b Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : gniccolai Date : Thu Feb 17 2005 03:20 am gottlobfrege@gmail.com wrote in message news:<1108573119.999831.204930@g14g2000cwa.googlegroups.com>... > Giancarlo Niccolai wrote: > > ... > > as pthread_once does not need > > to use a mutex, and usually (in well built implementations) it does > > not. But there are other problems, that I won't discuss with you. > ... > > Gian. > > Will you discuss it with me? I've been looking at versions of 'once' > lately (ie boost's and my own) and would be interested in any problems > with pthread_once, as well as versions that don't use a mutex. (Mine, > under Windows, appears to only need a Critical Section and a few Atomic > operations, which, I hope is slightly better than boost's version, > which uses a global named mutex...) > Yes. Pthread_once (and things with the same semantic) will execute a certain code just once. So, in static initializer context, the first thread passing on will begin to initialize the object; other threads crossing the same code section will skip what pthread_once does and will get the object; but the first thread may well not have fully initialized it (or in case of a COFU thing, it may not even have it placed in its final destination variable), so accesses to the object may bang your program. I.e., a thread may wish to add an element at the end of a list in the static initialized object, but the first thread may have not been able to create the list yet. Gian. . 0