0

I have a boost::shared_ptr<Map>, and this map is being modified by multiple threads, do i need to use explicit mutex or just the fact that threads are modifying the map through shared pointer, i will get some sort of implicit thread safety?

4
  • 4
    No. shared_ptr absolutely does not do anything for thread safety. All it does is provide reference-counting semantics around a dynamically allocated object. Commented Aug 23, 2012 at 23:53
  • 1
    The short answer to your question is "no". The thread safety that shared_ptr provides is only for the pointer itself. It doesn't do anything to protect the object itself. Once you get a pointer to the underlying object out of the shared_ptr structure, no more magic happens for you. Commented Aug 24, 2012 at 0:00
  • @DavidSchwartz I always access objects via indirection to shared_ptr and not through the raw pointer. Would that offer thread safety ? Commented Aug 24, 2012 at 12:36
  • @Jimm: No. All the shared_ptr does is give you the pointer to the object. Nothing would stop one thread from modifying the object through that pointer while another was reading it. The shared_ptr's thread safety protects the system of shared pointers only, not the underlying object. (For example, it ensures sanity if two threads throw away their smart pointers to the same object at the same time or if one thread gets a regular pointer from a smart pointer at the same time another thread throws away a smart pointer to the same object and so on.) Commented Aug 24, 2012 at 12:41

2 Answers 2

2

Did you even look at the docs?

shared_ptr objects offer the same level of thread safety as built-in types. A shared_ptr instance can be "read" (accessed using only const operations) simultaneously by multiple threads. Different shared_ptr instances can be "written to" (accessed using mutable operations such as operator= or reset) simultaneosly by multiple threads (even when these instances are copies, and share the same reference count underneath.)

Any other simultaneous accesses result in undefined behavior.

Edit: It looks like you're asking about the object pointed to, not the pointer itself. There are no thread safety gaurentees at all in that case. Why would there be?

Sign up to request clarification or add additional context in comments.

1 Comment

If there is any possibility that the pointer you're reading is changing, then it is not safe to read it, much less than a regular pointer. A regular pointer is one word, a shared pointer is a pointer to the object and a separate pointer to the count. So reading from it is not inherently atomic on any architecture. Therefor one problem is that you could read a value and a counter that don't match... Another problem is that if the shared pointer changes, it's possible that the object will be deleted in another thread part way through reading it. The increment to the ref-count could corrupt.
1

shared_ptr has the same level of thread safety as built-in types.

You can perform read (const) operations from multiple threads simultaneously.

You can also perform write operations (i.e. use mutable operations such as operator= or reset) on different shared_ptr instances simultaneosly from multiple threads. This includes the case when these instances share the same reference count (because shared_ptr provides atomic increment/decrement for the ref counter).

If you need any other type of access, you will need to synchronize it or otherwise you'll get undefined behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.