Are the following operations lockfree for std::unique_ptr
and/or std::shared_ptr
?
- Dereferencing, i.e.
read(*myPtr)
ormyPtr->getSomething()
- Removing a reference, i.e. with
std::move(myUniquePtr)
or when astd::shared_ptr
goes out of scope.
In my case, I am not concurrently accessing these pointers from multiple threads. I'm just curious if I can use them exclusively on a high-priority, lockfree thread. The objects managed by the pointers were allocated by the main thread prior to the high-priority callbacks and will not be deallocated until the callbacks cease.
Thanks!
move
and going out of scooe are very different.move
never changes the reference count. – Shoppermove
remove the reference from a sourceunique_ptr
and add it to a destinationunique_ptr
? I've seenstd::remove_reference
in errors related to moving aunique_ptr
. – Archenemyunique_ptr
has no need to ever lock: it has no significant contention. Forshared
, you can move the reference to the dest without doing a +1 -1 mess... without messimg with the control block comtents of the source at all! The dest admittedly needs a.reset()
of non-empty, so there is that. – Shopper