Hi Daniel,

Your proposed solution looks like a variant of the Double Checked Locking. 
Unfortunately, this design pattern is very subtly broken and not thread safe. 
See http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf for more 
information.

I've however shown this example just to illustrate a more general problem, as 
most of the getenv() are (in)directly called from within class constructors. 
Hence, what we're looking as is something more similar to the following.

static Mutex s_mutex;

Object & getSingletonObject()
{
    ScopedLock lock(s_mutex);

    static Object object;

    return object;
}


Cheers,

Tanguy


-----Original Message-----
From: osg-users-boun...@lists.openscenegraph.org 
[mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of Daniel 
Trstenjak
Sent: 06 August 2009 12:24
To: OpenSceneGraph Users
Subject: Re: [osg-users] Deadlock when loading osg.dll, singletons are evil


Hi Tanguy,

> static Mutex s_mutex;
> 
> const char * getSingletonGlExtensionDisable()
> {
>     ScopedLock lock(s_mutex);
> 
>     static const char * env = getenv("OSG_GL_EXTENSION_DISABLE");
> 
>     return env;
> }

This doesn't make sense, because 'getenv' is called only once, during
the initialization of the static variable 'env'. After that the
value of 'env' doesn't change, but for every call to
'getSingletonGlExtensionDisable' a lock is used.

If the value of 'OSG_GL_EXTENSION_DISABLE' shouldn't change during
runtime:

static Mutex s_mutex;

const char* getSingletonGlExtensionDisable()
{
   static char* env = 0L;
   if (env == 0L) {
      ScopedLock lock(s_mutex);
      env = getenv("OSG_GL_EXTENSION_DISABLE");
   }

   return env;
}




Greetings,
Daniel


-- 
                                                                                
                                                           
 Daniel Trstenjak         Tel   : +49 (0)7071-9457-264
 science + computing ag   FAX   : +49 (0)7071-9457-511
 Hagellocher Weg 73       mailto: daniel.trsten...@science-computing.de
 D-72070 Tübingen         WWW   : http://www.science-computing.de/              
                                                        
-- 
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Dr. Roland Niemeier, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Michel Lepert
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196 


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to