
divick kishore wrote:
Hi, I have several questions related to boost.
1. Are boost::smart pointers thread safe especially shared_ptr ? Although it seems but I still want to be sure of that.
Yes, in that you can safely copy pointers in multiple threads at the same time, i.e. access to the reference count is thread-safe. No, in that you cannot use a single smart pointer object simultaneously from several threads.
2. Why isn't there two flavours of every smart pointers, one with multithread safety and one without. If one is using some smart pointers in non threaded mode, then still does one need to pay the penalty for exclusive access ?
First, because the synchronization overhead of the current shared_ptr is small enough that it's not a reason to worry in most situations. Second, because experience has shown that having different types with different thread safety rules leads to chaos. Code that is single-threaded now might not remain so in the future. How many uses of the single-threaded versions are you going to forget to change? What if you refactor the code into a library? Will all code that calls this library be single-threaded? It's a tradeoff, and Boost chose to always be thread-safe. Sebastian Redl