In the absence of multithreading, the implementation of copy-on-write for
shared_ptr
(either from boost or tr1) using unique()
is
straightforward. Which changes need to be made when multithreading?
The reference count is atomic so I assume I can create, copy-construct,
read and destroy instances of shared_ptr
without further concerns.
How about updating them, in general, and particularly when implementing
copy-on-write? Are locks needed? Or use boost::atomic_store
(why is it not documented)? Or wait for a fully atomic version of
shared_ptr
(not an option)?
Edit:
sfossen, thank you for your helpful reply.
So I conclude that if I change a pointed-to object only after detaching
it via COW, so that only the current thread owns it, no locking is needed
and the COW implementation looks just like the single-threaded one
when using shared_ptr with atomic ref-counts.