
18 Jan
2005
18 Jan
'05
11:51 a.m.
Stefan Slapeta wrote:
Roland Schwarz wrote:
[...]
The broader question is: Can local static objects ever be made thread safe? Or should they be avoided at all in MT aware code?
1) No 2) Yes!!
Local static objects can be made safe by the compiler. The new cxx64 API requires it. They can also be made safe by using one of the following two approaches (pseudocode): 1. mutex m = MUTEX_INITIALIZER; void f() { mutex_lock(&m); static X x; mutex_unlock(&m); // use x } 2. X & instance() { static X x; return x; } void f() { call_once( instance ); X /*const*/ & x = instance(); // use x }