C++ and the Perils of Double-Checked Locking: Part II
By Scott Meyers and Andrei Alexandrescu, August 01, 2004
In this installment, Scott and Andrei examine the relationship between thread safety and the volatile
keyword.
August, 2004: C++ and the Perils of Double-Checked Locking: Part II
if (pInstance == 0) {
Lock lock;
if (pInstance == 0) {
Singleton* volatile temp =
static_cast<Singleton*>(operator new(sizeof(Singleton)));
temp->x = 5; // inlined Singleton constructor
pInstance = temp;
}
}
Example 8: Inlining the constructor in Example 7.