Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 586
Christopher Tarquini Avatar asked Jan 28 '15 18:01

Christopher Tarquini


1 Answers

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

like image 181
Howard Hinnant Avatar answered Nov 03 '22 11:11

Howard Hinnant