DaKoadMunky wrote:
note that there may be problems with thread safety in some
implementations. gcc 4.0 will have a fix for this, I'm not sure about
VC++ 2005 though.
Does the standard require it to be thread safe?
If not then making it thread safe is really not a fix, it is just a courtesy.
Some people will ague that the standard mentions nothing about threads
and hence it is up to the programmer to make things thread safe.
Some will argue that due to the lack of mentioning anything about
threads, the compiler is required to make certain operations thread safe
if the compiler has any extensions that allow threads.
IMHO, the precedent is that many of the standard C++ types are
implemented in a thread safe way today when they were not in the past.
(e.g. std::string was not thread safe in gcc 2.96 but it has been since
gcc 3.0)
The standard is quite explicit that local static variables must be
constructed once. If your compiler allows any extension that enables
threads, then the compiler must generate code that conforms to the
standard (hence thread safe construction of local static variables).
Will the thread safety of such a construct on gcc 4.0 be optional so that one
does not pay for the cost of unnecessary synchronization for single-threaded
programs?
The gcc-4.0 changes (
http://gcc.gnu.org/gcc-4.0/changes.html) says :
- The compiler now uses the library interface specified by
the C++ ABI for thread-safe initialization of function-scope
static variables. Most users should leave this alone, but
embedded programmers may want to disable this by specifying
-fno-threadsafe-statics for a small savings in code size.
It seems that -fno-threadsafe-statics will revert to the old (IMHO buggy
behaviour if you use threads).