Boost documentation describes shared pointer's behavior when accessing it from multiple threads simultaneously. Particularly they give some examples:
shared_ptr<int> p(new int(42));
//--- Example 1 ---
// thread A
shared_ptr<int> p2(p); // reads p
// thread B
shared_ptr<int> p3(p); // OK, multiple reads are safe
//--- Example 2 ---
// thread A
p.reset(new int(1912)); // writes p
// thread B
p2.reset(); // OK, writes p2
//--- Example 3 ---
// thread A
p = p3; // reads p3, writes p
// thread B
p3.reset(); // writes p3; undefined, simultaneous read/write
...
But they do not say (or I cannot see) what will happen if the same shared_ptr
object is written and read at the same time. Say:
shared_ptr<int> p(new int(42));
//--- My Example ---
// thread A
p.reset(new int(1912));
// thread B
shared_ptr<int> p1 = p;
So my question is whether the last example is OK or not?
NOTE: Boost's third example explains that it is not safe to read and write the same object in parallel. But in their example they assign p3
to p
, which is a copy of p3
. So it is not clear to me whether the safety of that example depends on the fact that p3
gets assigned to its copy or whether something else is unsafe.