Hi Guido,
Some uses of boost::shared_ptr
are thread safe, but other uses are not.
The documentation for shared pointers describes which operations are thread safe:
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.)
These are the two things you can do with a shared pointer: access it without changing it and modify separate shared pointer instances simultaneously. The realtime box solves a third use case: passing an instance of a shared pointer from one thread to another.
Consider the following scenario. Two threads share a global shared pointer named ptr
. Thread A writes to ptr
, and thread B reads from it:
=== Thread A
ptr.reset(new Object);
=== Thread B
my_ptr = ptr;
This is not guaranteed to be thread safe because shared pointer assignment is not atomic. The assignment to my_ptr
can occur at the same time that thread A is modifying ptr
, leading to a corrupted shared pointer.
If you're still doubtful, here is a stackoverflow question with a similar example.