Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Using Boost shared_ptr

beebs1
Beginner
405 Views
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.
0 Kudos
4 Replies
RafSchietekat
Valued Contributor III
405 Views
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.
0 Kudos
SergeyKostrov
Valued Contributor II
405 Views
Quoting beebs1
...I'm unsure whether shared pointers are thread-safe...


What doesthe Boost documentation say about it?

0 Kudos
RafSchietekat
Valued Contributor III
405 Views
"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. :-)
0 Kudos
SergeyKostrov
Valued Contributor II
405 Views
"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.

0 Kudos
Reply