std::shared_ptr thread safety explained

前端 未结 3 1802
情深已故
情深已故 2020-11-27 10:29

I\'m reading http://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html and some thread safety issues are still not clear for me:

  1. Standard guarantees that
3条回答
  •  庸人自扰
    2020-11-27 11:20

    1. Correct, shared_ptrs use atomic increments/decrements of a reference count value.

    2. The standard guarantees only one thread will call the delete operator on a shared object. I am not sure if it specifically specifies the last thread that deletes its copy of the shared pointer will be the one that calls delete (likely in practice this would be the case).

    3. No they do not, the object stored in it can be simultaneously edited by multiple threads.

    EDIT: Slight followup, if you want to get an idea of how shared pointers work in general you might want to look at the boost::shared_ptr source: http://www.boost.org/doc/libs/1_37_0/boost/shared_ptr.hpp.

提交回复
热议问题