From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ulrich Drepper To: mrs@wrs.com (Mike Stump) Cc: egcs@cygnus.com Subject: Re: threadsafeness at egcs1.0.3 and 1.1 Date: Mon, 13 Jul 1998 19:49:00 -0000 Message-id: References: <199807132228.PAA19571@kankakee.wrs.com> X-SW-Source: 1998-07/msg00468.html mrs@wrs.com (Mike Stump) writes: > No, this is defined in C++ as i having the value zero on the second > call until someone starts changing i. The implementation is required > to not block, pend or busy wait. We can extend this out to threaded > apps uniformly. The setting and testing of the single flag that > controls initialization should be critical regioned. No, this is certainly wrong. The handling must be as for recursive mutexes. With them it is allowed for one thread to allocate the mutex as often as it wants. It is used in situations where a certain region is protected using a mutex and calling one function in the region might call another one. A different thread has to wait to get the mutex and this is exactly what has to happen here. Assume this: void very_important_logging (string s) { static int pid = my_getpid (); if (pid == 0) /* Recursive call, don't do anything. */ return; cout << process_name[pid] << ": " << s << endl; } If my_getpid() can lead to an error which results in calling very_important_logging again the value of `pid' is 0 as you said and the thread must not block. In this case it simply returns and might do some reasonable work (maybe terminate the program or whatever). If a second thread would enter the scene which the other thread executes the my_getpid function it would simply fail to print anything and return and we loose an important message. I.e., given your interpretation (which is not founded by the standard since it does not handle threads at all) the value 0 would signal two things: the own thread executes the initializer and another thread executes the initializer. This is the differenciation made in the POSIX standard at it makes sense. Your interpretation wouldn't allow use to differentiate these two cases. On the other hand making the call to the initializer similar to a pthread_once call would mean that we a) can differentiate the cases, and b) keep the semantic of any code as if it would run in single threaded programs. -- ---------------. drepper at gnu.org ,-. 1325 Chesapeake Terrace Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA Cygnus Solutions `--' drepper at cygnus.com `------------------------