On Mon, 05 May 2008 20:30:28 +0200
Benoît Laniel <[EMAIL PROTECTED]> wrote:

> Le vendredi 02 mai 2008 à 10:34 +0200, Tim Moore a écrit :
> > This concern is valid, but *every* use of SGMutex removed in
> > Benôit's patch is not actually thread safe now! Static local
> > variables are initialized the first time a function is called. I'm
> > not sure if the proper lesson to take away is that threading is
> > hard, because of subtle issues like this, or if it's easy, because
> > it works even in the presence of such bugs ;)
> 
> I looked at the code again and, if I understand your warning, there
> is 2 options:
> 
> 1/ I'm completely dumb (which is possible ;))
> 2/ The old code was not thread safe either.
> 
2) is correct. The problem is that a static local variable is only
guaranteed to be initialized when a function is first run. If a
function has not been called and two threads both call it at
the same time then there will be a race to initialized the mutex.

> Old code
> static SGMutex mutex;
> ------
> 
> New code
> static OpenThreads::Mutex mutex;
> ------
> 
I'm sorry for the misunderstanding; I didn't mean to imply that your
patch was more unsafe than the old code.

> As a side note, it seems you already use OpenThreads Mutexes in
> various places, like in line 939 of simgear/scene/model/animation.cxx:
> 
> OpenThreads::ScopedLock<OpenThreads::Mutex> lock(normalizeMutex);
> 
> or in line 72 of simgear/scene/util/StateAttributeFactory.cxx:
> 
> OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
> 
> Would this produce the correct behavior ?

In both those cases the mutex is a global variable, so it is
initialized before the main() function of flightgear is called. So
those uses are safe assuming that the functions in which they are
located are not called before main().

Tim

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to