Using Boost shared_ptr

Using Boost shared_ptr

Hiya,

We rely a lot on the Boost library, specifically the shared_ptr<> class.

Has anyone used shared pointers successfully with multiple threads and Intel TBB? I'm unsure whether shared pointers are thread-safe...

Any advice appreciated!

Cheers.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As long as you are aware of the limitations to the thread-safe use of shared_ptr instances, you'll be able to use them with TBB.

(Added) Also be aware that the administrative cost of managing those reference counts can easily outweigh savings in memory allocation, so be careful to consider when to share objects and when to just copy them.

Quoting beebs1...I'm unsure whether shared pointers are thread-safe...

What doesthe Boost documentation say about it?

"What doesthe Boost documentation say about it?"
Please click on the link provided in #1.

Regarding the cost of managing reference counts, it would probably even be a good idea to pass shared_ptr instances by const reference instead of by value, although I would prefer to see someone else's anecdotal evidence for that (time program with lots of pass-by-value, correct it, time again, compare), or a reference to somebody else's study about this. :-)

Quoting Raf Schietekat"What doesthe Boost documentation say about it?"
Please click on the link provided in #1.
...

Itsays:

...
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.)
...

Thank you.

Leave a Comment

Please sign in to add a comment. Not a member? Join today