简体   繁体   English

为什么libc ++的shared_ptr实现使用全内存屏障而不是放松?

[英]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.

相关问题 为什么 libc++ 的 shared_ptr 实现使用 unique_ptr 来保存原始指针构造函数中的原始指针? - Why does libc++'s implementation of shared_ptr use a unique_ptr to hold the raw pointer in the raw-pointer constructor? std :: shared_ptr的use_count()周围的全部内存屏障是否会使它成为可靠的计数器? - Will full memory barriers around std::shared_ptr's use_count() make it a reliable counter? 为什么libc ++的map实现使用此联合? - Why does libc++'s implementation of map use this union? 为什么 libc++ 的 std::string 实现占用 3 倍于 libstdc++ 的内存? - Why does libc++'s implementation of std::string take up 3x memory as libstdc++? libc ++不允许构造shared_ptr <T> 来自unique_ptr <T[]> - libc++ doesn't allow construction of shared_ptr<T> from unique_ptr<T[]> 为什么shared_ptr <void>而不是shared_ptr <HANDLE> - Why shared_ptr<void> instead of shared_ptr<HANDLE> 为什么 shared_ptr 不删除它的 memory? - Why does shared_ptr not delete its memory? 为什么shared_ptr<T> ::use_count() 返回 long 而不是无符号类型? - Why does shared_ptr<T>::use_count() return a long instead of an unsigned type? 在 GCC 上使用 libc++ 标准库实现而不是 libstdc++ - Use libc++ standard library implementation on GCC instead of libstdc++ Shared_ptr 实现 - Shared_ptr implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM