1

At CppCon 2014, Herb Sutter described a neat solution to the ABA problem using atomic shared ptr. A summary of this solution can be found at the bottom of this article. However, the partial specialization of atomic on shared_ptr is a feature of the incoming C++20 (see here). Is there a way to solve the problem elegantly using C++14?

Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • I imagine the deprecated free functions may be of use http://en.cppreference.com/w/cpp/memory/shared_ptr/atomic – StoryTeller - Unslander Monica Feb 12 '18 at 13:44
  • cant you write your own specializations already in c++14? – 463035818_is_not_an_ai Feb 12 '18 at 13:45
  • @StoryTeller The first note is really disappointing :( *These functions are typically implemented using mutexes, stored in a global hash table where the pointer value is used as the key.* – Lingxi Feb 12 '18 at 13:53
  • 1
    @user463035818: no, the template needs to know about `shared_ptr` internals. Plain `shared_ptr` is not trivially-copyable, so you can't create a plain `atomic` without the library providing a specialization of the template that does things you can't portably do with a `shared_ptr` in vanilla ISO C++11 / 14. – Peter Cordes Feb 12 '18 at 16:22
  • 1
    @Lingxi: A hash table of mutexes is how non-lockfree `atomic<>` objects are typically implemented even for normal types. (Note that collisions simply mean two things exclude each other when they don't need to, so collisions in the table usually don't lead to extra hash searching, they usually lead to otherwise-independent things locking each other out. Normally that's not a problem). You can expect it to be fairly well optimized, but yeah it's not lockless. – Peter Cordes Feb 12 '18 at 16:26
  • @PeterCordes I tried to implement one myself, and have posted on [CodeReview](https://codereview.stackexchange.com/q/188097/70823), hope you could take a look and comment :) – Lingxi Feb 22 '18 at 10:25
  • @user463035818 Tried, but not quite confident it's correct. Have posted on [CodeReview](https://codereview.stackexchange.com/q/188097/70823) for comments. – Lingxi Feb 22 '18 at 10:27

0 Answers0