Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Joseph Seigh Date : Thu Jan 20 2005 01:01 pm On Thu, 20 Jan 2005 09:23:19 -0800, Gianni Mariani wrote: > 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. > Missing read memory barrier to ensure reads of data occur after read of pointer to data. Also write memory barrier to ensure writes of data occur before write of pointer. The lock doesn't solve either of those problems. There's a couple of solutions to this but I don't have all the urls instantly on hand like Alexander seems to have. -- Joe Seigh .