Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : David Schwartz Date : Fri Feb 11 2005 12:20 pm "Bruce Visscher" wrote in message news:1108151367.547058.55560@o13g2000cwo.googlegroups.com... >> 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? It *might*. But don't you want to write code that guaranteed to work because it complies with the relevant standards rather than code that just happens to work because you fixed all the problems that were biting you but left the ones that didn't happen to bite you that day, on that compiler, on that platfor, with that data set? >> 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. I think the '__thread' keyword was meant to mark a thread-local variable. If you don't have it, you need to use whatever thread-local variable support you do have. > 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. Sounds like you're trying to code to a particular platform. You'll need to find someone with specialized knowledge about that platform, and your code will not be portable. DS .