apparently the above code isn't thread-safe. even if you wrap it around with critical sections, C++ generates code for the static variable like "if not null return" stuff, and since it's in the same critical section it's possible to return a null variable when it's already initialized.Code:static T& Instance() { static T t; return t; }
is the following code thread-safe?
Code:class Foo { public: static T& Instance() { return _t; } private: static T _t; };