I would like to post my comment for the reference counting in the boost shared pointer in the multiple threads use cases. The comment is to answer the question that “is there any race condition in the boost shared pointer reference counting?”
My simple answer is “No” at least after boost 1.35 for most mainstream compiler. The boost implementation called “add_ref_copy” defined in the boost/detail/shared_count.hpp. This function will invoke the corresponding atomic function defined for individual compiler. For example, the windows version will call “BOOST_INTERLOCKED_INCREMENT” to increment the count in the atomic way (see details in detail\sp_counted_base_w32.hpp). And the Linux gcc for X86 will call atomic_increment(… ) (see details in detail\sp_counted_base_gcc_x86.hpp). Each individual compiler implemented the thread-safe mechanism to make sure the reference counting update in an efficient way. Some piece of code are even written in assembly.
Now there is a caveats in my simple answer. You really need to make sure your compiler is included in the boost’s blessed list for multiple thread-safe reference counting. If you are not sure you can define “BOOST_SP_USE_PTHREADS” which drives boost to use the pthread library to make reference count update atomically (by including boost/detail/sp_counted_base_pt.hpp for pthread solution).