[英]Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?
In boost's implementation of shared_ptr
, it uses relaxed memory ordering to increment its reference count . 在boost的
shared_ptr
实现中,它使用宽松的内存排序来增加其引用计数 。 This appears safe as decrements use acquire/release to make sure that any previous decrements are visible to the thread before releasing memory. 这似乎是安全的,因为减量使用获取/释放来确保在释放内存之前线程可以看到任何先前的减量。 This method seems correct and appears in Herb Sutters talk on atomics
这种方法似乎是正确的,并出现在Herb Sutters 谈论原子论
In libc++'s implementation uses full memory barriers 在libc ++中,实现使用全内存屏障
template <class T>
inline T
increment(T& t) _NOEXCEPT
{
return __sync_add_and_fetch(&t, 1);
}
template <class T>
inline T
decrement(T& t) _NOEXCEPT
{
return __sync_add_and_fetch(&t, -1);
}
} // name
Is there a reason for this decision? 这个决定有理由吗? Are there any performance or safety differences between them?
它们之间是否有任何性能或安全差异?
Because when I wrote that code, the compiler (clang) had not yet implemented C++11 atomics. 因为当我编写该代码时,编译器(clang)还没有实现C ++ 11原子。 And I never got back to it to clean it up.
我从来没有回过头去清理它。
Nothing subtle here. 没有什么微妙的。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.