Does anybody know of a fully thread-safe shared_ptr
implementation? E.g. boost implementation of shared_ptr
is thread-safe for the targets (refcounting) and also safe for simultaneous shared_ptr
instance reads, but not writes or for read/write.
(see Boost docs, examples 3, 4 and 5).
Is there a shared_ptr implementation that is fully thread-safe for shared_ptr
instances?
Strange that boost docs say that:
shared_ptr objects offer the same level of thread safety as built-in types.
But if you compare an ordinary pointer (built-in type) to smart_ptr
, then simultaneous write of an ordinary pointer is thread-safe, but simultaneous write to a smart_ptr
isn't.
EDIT: I mean a lock-free implementation on x86 architecture.
EDIT2: An example use case for such a smart pointer would be where there are a number of worker threads which update a global shared_ptr with a their current work item and a monitor thread that takes random samples of the work items. The shared-ptr would own the work item until another work item pointer is assigned to it (thereby destroying the previous work item). The monitor would get ownership of the work item (thereby preventing the work item to be destroyed) by assigning it to its own shared-ptr. It can be done with XCHG and manual deletion, but would be nice if a shared-ptr could do it.
Another example is where the global shared-ptr holds a "processor", and is assigned by some thread, and used by some other thread. When the "user" thread sees that the processor shard-ptr is NULL, it uses some alternative logic to do the processing. If it's not NULL, it prevents the processor from being destroyed by assigning it to its own shared-ptr.