我正在阅读 http ://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html 和一些线程安全问题对我来说仍然不清楚:
- 标准保证引用计数的处理是线程安全的并且它是独立于平台的,对吗?
- 类似的问题 - 标准保证只有一个线程(保存最后一个引用)会调用共享对象上的删除,对吧?
- shared_ptr 不保证其中存储的对象的任何线程安全吗?
编辑:
伪代码:
// Thread I
shared_ptr<A> a (new A (1));
// Thread II
shared_ptr<A> b (a);
// Thread III
shared_ptr<A> c (a);
// Thread IV
shared_ptr<A> d (a);
d.reset (new A (10));
在线程 IV 中调用 reset() 将删除在第一个线程中创建的 A 类的先前实例并将其替换为新实例?此外,在 IV 线程中调用 reset() 后,其他线程只会看到新创建的对象?
I'm reading http://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html and some thread safety issues are still not clear for me:
- Standard guarantees that reference counting is handled thread safe and it's platform independent, right?
- Similar issue - standard guarantees that only one thread (holding last reference) will call delete on shared object, right?
- shared_ptr does not guarantee any thread safety for object stored in it?
EDIT:
Pseudo code:
// Thread I
shared_ptr<A> a (new A (1));
// Thread II
shared_ptr<A> b (a);
// Thread III
shared_ptr<A> c (a);
// Thread IV
shared_ptr<A> d (a);
d.reset (new A (10));
Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will see only newly created object?
发布评论
评论(3)
正如其他人指出的那样,您已经正确地解决了最初的 3 个问题。
但你编辑的结尾部分
是不正确的。只有
d
会指向新的A(10)
,以及a
、b
和c
将继续指向原来的A(1)
。从下面的简短示例中可以清楚地看出这一点。(显然,我没有关心任何线程:这不会影响
shared_ptr::reset()
行为。)此代码的输出是
As others have pointed out, you've got it figured out correctly regarding your original 3 questions.
But the ending part of your edit
is incorrect. Only
d
will point to the newA(10)
, anda
,b
, andc
will continue to point to the originalA(1)
. This can be seen clearly in the following short example.(Clearly, I didn't bother with any threading: that doesn't factor into the
shared_ptr::reset()
behavior.)The output of this code is
正确,
shared_ptr
使用引用计数值的原子增量/减量。该标准保证只有一个线程会对共享对象调用删除运算符。我不确定它是否明确指定删除其共享指针副本的最后一个线程将是调用删除的线程(实际上可能是这种情况)。
不,他们没有,其中存储的对象可以由多个线程同时编辑。
编辑:稍微跟进一下,如果您想了解共享指针一般如何工作,您可能需要查看
boost::shared_ptr
源:https://www.boost.org/doc/libs/release/boost/smart_ptr/shared_ptr.hpp。Correct,
shared_ptr
s use atomic increments/decrements of a reference count value.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).
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: https://www.boost.org/doc/libs/release/boost/smart_ptr/shared_ptr.hpp.std::shared_ptr
不是线程安全的。共享指针是一对两个指针,一个指向对象,一个指向控制块(保存引用计数器,链接到弱指针......)。
可以有多个 std::shared_ptr ,每当它们访问控制块以更改引用计数器时,它都是线程安全的,但 std::shared_ptr 本身不是线程安全的或原子的。
如果您将一个新对象分配给 std::shared_ptr 而另一个线程使用它,则它最终可能会得到新对象指针,但仍然使用指向旧对象控制块的指针 =>碰撞。
std::shared_ptr
is not thread safe.A shared pointer is a pair of two pointers, one to the object and one to a control block (holding the ref counter, links to weak pointers ...).
There can be multiple std::shared_ptr and whenever they access the control block to change the reference counter it's thread-safe but the
std::shared_ptr
itself is NOT thread-safe or atomic.If you assign a new object to a
std::shared_ptr
while another thread uses it, it might end up with the new object pointer but still using a pointer to the control block of the old object => CRASH.