Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because <code>volatile</code> is not necessary for multithreading, and does nothing beneficial, but potentially destroys a number of optimizations.</p> <p>In order to ensure safe multithreaded access to a variable, the primitive we need is a memory barrier, which provides both the guarantee of <code>volatile</code> and a few others (it prevents memory access reordering across the barrier, which volatile doesn't do)</p> <p>I believe <code>shared_ptr</code> uses atomic operations when possible, which implicitly provide a memory barrier. Otherwise it falls back to a mutex, which also provides a memory barrier.</p> <p>See <a href="https://stackoverflow.com/questions/2484980/why-is-volatile-not-considered-useful-in-multithreaded-c-or-c-programming">Why is volatile not considered useful in multithreaded C or C++ programming?</a> or <a href="http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/" rel="nofollow noreferrer">http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/</a> for more details</p> <p><strong>Edit</strong><br> <code>count_type</code> is <em>not</em> a <code>long</code> in the general case. It is <em>convertible</em> to a <code>long</code>. If you look in <code>atomic_count.hpp</code>, the typedef to long is only applied if no threading is available (in which case of course, no synchronization is necessary). Otherwise it uses the implementation defined in <code>boost/smart_ptr/detail/atomic_count_pthreads.hpp</code> or <code>boost/smart_ptr/detail/atomic_count_win32.hpp</code> or one of the other files listed. And those are synchronized wrapper classes that ensures all operations are done atomically.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload