Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?

别说谁变了你拦得住时间么 提交于 2021-02-17 12:22:38

问题


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

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?


回答1:


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. :-)



来源:https://stackoverflow.com/questions/28199212/why-does-libcs-implementation-of-shared-ptr-use-full-memory-barriers-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!