
AMDG Andrew Chinkoff wrote:
Ok, I see. I would try to explain.
Let us look at pseudo code:
*** BEGIN PSEUDO CODE *** static int* my_int = 0; static Mutex mutex;
void CreateInt() { if(!my_int) { Locker locker(mutex); if(!my_int) my_int = new int(100); } }
void thread_func() { boost::call_once(CreateInt, flag); printf("[%d]", *my_int); }
void main() { th1 = create_thread(thread_func); th2 = create_thread(thread_func); while(1) { // Prints "[0][100]", or "[100][0]", or "[100][100]" - hm... undefined! } } *** END OF PSEUDO CODE ***
There is no race in this code. It will always print [100][100]. In fact, the code would work equally well if you said: void CreateInt() { my_int = new int(100); } In Christ, Steven Watanabe