C++ volatile required when spinning on boost::shared_ptr operator bool()?
Posted
by
JaredC
on Stack Overflow
See other posts from Stack Overflow
or by JaredC
Published on 2011-01-11T20:49:23Z
Indexed on
2011/01/11
20:53 UTC
Read the original article
Hit count: 177
I have two threads referencing the same boost::shared_ptr
:
boost::shared_ptr<Widget> shared;
On thread is spinning, waiting for the other thread to reset the boost::shared_ptr
:
while(shared)
boost::thread::yield();
And at some point the other thread will call:
shared.reset();
My question is whether or not I need to declare the shared pointer as volatile
to prevent the compiler from optimizing the call to shared.operator bool()
out of the loop and never detecting the change? I know that if I were simply looping on a variable, waiting for it to reach 0 I would need volatile
, but I'm not sure if boost::shared_ptr
is implemented in such a way that it is not necessary here.
© Stack Overflow or respective owner