Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : David Hopwood Date : Sat Feb 05 2005 02:28 am Marcin 'Qrczak' Kowalczyk wrote: > Giancarlo Niccolai writes: > >>>Please show how it's broken when thread local static objects are >>>initialized in a thread-aware way, and at the same time it works >>>when they are initialized using an traditional guard variable. >> >>Thread-aware way do not mean locked; it may require locking it may >>not require it, it may suffice or not. The BAD thing is that THIS >>kind of blind-locking may get in the way of the CORRECT way to >>secure your code, preventing you from really doing so. I am sorry, >>but if you don't get this, I cannot absolutely do nothing more to >>help you. > > I'm saying that you are wrong, and this kind of locking doesn't get > in the way - sometimes it helps, sometimes it doesn't help, but it > never makes things worse except efficiency. > > I will happily admit my error if there is indeed some non-perverse > code which is being broken by this semantics of local static > initialization. You have been unable to show sketch such code, and > I can't imagine how it may look, so I claim that it doesn't exist > until it's shown otherwise. Just for fun, I tried to construct an artificial (and certainly perverse) counterexample by trying to get cyclically dependent constructors to terminate in a single-threaded program. However, this segfaults anyway, at least with g++. Perhaps someone can "fix" it. test.cc: #include using namespace std; int i = 10; class Foo { public: Foo(); }; class Bar { public: Bar() { if (i > 0) { static Foo foo; } } }; Foo::Foo() { i--; static Bar bar; } int main(int argc, char **argv) { cout << "hello\n"; Foo(); cout << "got here\n"; return 0; } $ g++ --version g++ (GCC) 3.2 20020818 (prerelease) Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ g++ -ansi -pedantic -Wall test.cc -o test $ ./test hello Segmentation fault (core dumped) -- David Hopwood .