Is boost shared_ptr thread safe?

后端 未结 5 584
灰色年华
灰色年华 2020-12-01 03:22

I have a question about boost::shared_ptr.

There are lots of thread.

using namespace boost;

class CResource
{
  // xxxxxx
}

c         


        
相关标签:
5条回答
  • 2020-12-01 04:06

    m_Res is not threadsafe ,because it simultaneous read/write, you need boost::atomic_store/load function to protects it.

    //--- Example 3 ---
    // thread A
    p = p3; // reads p3, writes p
    // thread B
    p3.reset(); // writes p3; undefined, simultaneous read/write
    
    0 讨论(0)
  • 2020-12-01 04:08

    boost::shared_ptr<> offers a certain level of thread safety. The reference count is manipulated in a thread safe manner (unless you configure boost to disable threading support).

    So you can copy a shared_ptr around and the ref_count is maintained correctly. What you cannot do safely in multiple threads is modify the actual shared_ptr object instance itself from multiple threads (such as calling reset() on it from multiple threads). So your usage is not safe - you're modifying the actual shared_ptr instance in multiple threads - you'll need to have your own protection.

    In my code, shared_ptr's are generally locals or parameters passed by value, so there's no issue. Getting them from one thread to another I generally use a thread-safe queue.

    Of course none of this addresses the thread safety of accessing the object pointed to by the shared_ptr - that's also up to you.

    0 讨论(0)
  • 2020-12-01 04:10

    Well, tr1::shared_ptr (which is based on boost) documentation tells a different story, which implies that resource management is thread safe, whereas access to the resource is not.

    "...

    Thread Safety

    C++0x-only features are: rvalue-ref/move support, allocator support, aliasing constructor, make_shared & allocate_shared. Additionally, the constructors taking auto_ptr parameters are deprecated in C++0x mode.

    The Thread Safety section of the Boost shared_ptr documentation says "shared_ptr objects offer the same level of thread safety as built-in types." The implementation must ensure that concurrent updates to separate shared_ptr instances are correct even when those instances share a reference count e.g.

    shared_ptr a(new A); shared_ptr b(a);

    // Thread 1 // Thread 2

    a.reset(); b.reset();

    The dynamically-allocated object must be destroyed by exactly one of the threads. Weak references make things even more interesting. The shared state used to implement shared_ptr must be transparent to the user and invariants must be preserved at all times. The key pieces of shared state are the strong and weak reference counts. Updates to these need to be atomic and visible to all threads to ensure correct cleanup of the managed resource (which is, after all, shared_ptr's job!) On multi-processor systems memory synchronisation may be needed so that reference-count updates and the destruction of the managed resource are race-free.

    ..."

    see http://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.shared_ptr

    0 讨论(0)
  • 2020-12-01 04:17

    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.

    0 讨论(0)
  • 2020-12-01 04:19

    Add, your class has a Cyclic-references condition; the shared_ptr<CResource> m_Res can't be a member of CResourceBase. You can use weak_ptr instead.

    0 讨论(0)
提交回复
热议问题