C/C++
cpplock2.txt
Associated article: C++ and the Perils of Double-Checked Locking: Part II
Tags: Tools C/C++
Published source code accompanying Part II of the article by Scott Meyers and Andrei Alexandrescu in which they examine the C++ and the Double-Checked Locking pattern. This month they examine the relationship between thread safe and the volatile keyword.
C++ and Double-Checked Locking: Part II
by Scott Meyers and Andrei Alexandrescu
Example 7:
class Singleton {
public:
static Singleton* instance();
...
private:
static Singleton* volatile pInstance; // volatile added
int x;
Singleton() : x(5) {}
};
// from the implementation file
Singleton* Singleton::pInstance = 0;
Singleton* Singleton::instance() {
...


