"is it thread safe to reset and copy shared_ptr simultaneously?" Code Answer


but they does not say (or i cannot see) what will happen if the same shared_ptr object will be written and read at the same time.

yes they do:

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) simultaneously by multiple threads (even when these instances are copies, and share the same reference count underneath.)

any other simultaneous accesses result in undefined behavior.

(emphasis mine)

seems pretty clear to me.

so my question is whether the last example ok or not?

no, because what you are doing is not reading simultaneously from a single instance, nor writing to separate instances. you are simultaneously reading and writing a single instance. that's a data race, and undefined behaviour.

By Matteo Tessarotto on April 25 2022

Answers related to “is it thread safe to reset and copy shared_ptr simultaneously?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.