Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Bruce Visscher Date : Fri Feb 11 2005 11:49 am > Usually "__thread" makes a variable thread-local. Thanks for explaining that. > Whereas 'volatile' is used when a variable might be modified from > a signal handler. Yes, but doesn't it also inhibit certain compiler optimizations that might be causing the code in question to fail as I (think I) have observed? > If you're trying to access a variable in a thread-safe way under > UNIX, it's usually thread pthread_mutex functions that you want. Sorry. I guess I wasn't clear. The problem is: what I think I saw code similar to Gianni Mariani's MTSafeStatic macro (but without the __thread keyword!) fail inspite of the fact that it *has* pthread_mutex locking already(*). Which, from studying this thread is starting to make some sense. What I am asking is whether I can fix this using volatile instead of __thread. There will be a pthread mutex involved in either case. If not then maybe I need to rely on atomic operations instead. I actually think the VMS platform would be better off with the more naive looking: > Item &cofu() { > MTSafeStatic( Item, the_item, ); > return the_item; > } which IIRC, CXX on VMS will make threadsafe like GCC 4.0 does (maybe in a different manner). I am seeking a more portable solution. (*) I have a SingletonLock class that I use for this which utlizes pthread_once to avoid a race condition with the initialization of the mutex. .