(Assuming VC++ 2010: (1) can use /volatile:ms, (2) no std::atomic yet, (3) no thread-safe static variable initialization, (4) no std::call_once)
If I have a plain C pointer, I can impl the following double checked lock pattern to avoid the cost of lock every time:
static volatile void * ptr = nullptr;
//...
if ( ptr == nullptr)
{
// Acquire Lock
if (ptr == nullptr)
{
// some code
// ptr = ...; // init ptr
}
// Release Lock
}
// ....
Since VC++ 2005, the volatile makes sure the above code is correct. Assume I'm OK with the code being not portable.
Now assume I need to replace the plain pointer with a std::shared_ptr or boost::shared_ptr, how would I do the same thing? How to make that shared_ptr volatile? Do I need another volatile flag?