"is boost shared_ptr <xxx> thread safe?" Code Answer

5

from the boost documentation:

shared_ptr objects offer the same level of thread safety as built-in types. 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.

so your usage is not safe, since it uses simultaneous read and write of m_res. example 3 in the boost documentation also illustrates this.

you should use a separate mutex that guards the access to m_res in setresource/getresource.

By Mercutio Calviary on January 9 2022

Answers related to “is boost shared_ptr <xxx> thread safe?”

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