为什么libc ++的shared_ptr实现使用完整的内存屏障而不是宽松的内存? [英] Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?
问题描述
在 shared_ptr
的boost实现中,它使用谈原子
In boost's implementation of shared_ptr
, it uses relaxed memory ordering to increment its reference count. 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
在libc ++的实现中,使用完整的内存屏障
In libc++'s implementation uses full memory barriers
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?
推荐答案
由于我编写该代码时,编译器(clang)尚未实现C ++ 11原子.而且我从来没有回过头来清理它.
Because when I wrote that code, the compiler (clang) had not yet implemented C++11 atomics. And I never got back to it to clean it up.
这里没有什么微妙的地方.:-)
Nothing subtle here. :-)
这篇关于为什么libc ++的shared_ptr实现使用完整的内存屏障而不是宽松的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!