Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Gianni Mariani Date : Thu Jan 20 2005 09:23 am Alexander Terekhov wrote: > Gianni Mariani wrote: > [...] > >>>[snip busted "DCL"] >> >>If it was unbusted, what would it look like ? > > > Unbusted, it would look like DCSI-MBR or DCSI-TLS/TSD (DCSI-SEMAS-01 > and DCSI-SEMAS-11 aside for a moment). > > http://www.google.de/groups?selm=40DFD511.A10D1DA0%40web.de > > regards, > alexander. Would this DCL work now ? #define MTSafeStatic( Type, Name, Initializer ) \ static Type * volatile x_ ## Name; \ \ if ( ! x_ ## Name ) \ { \ Lock l_lock( StaticInitializerMutex );\ \ if ( ! x_ ## Name ) \ { \ static Type v_ ## Name Initializer; \ \ x_ ## Name = & v_ ## Name ; \ } \ } \ \ Type & Name = * x_ ## Name; \ // End macro // Note that there must be exactly one StaticInitializerMutex // otherwise you will be making deadlock posssible when // you have recursive constructors The reason I think this may work is that the value of x_Name is idempotent and the only thing the lock is doing is to have threads wait for the initialization. .