javac++atomiclock-freeaba

Is there anything like Java's AtomicStampedReference in C++?


I am learning lock-free structure, and I noticed an ABA problem.

I think Java's AtomicStampedReference can solve this problem.

So, is there anything similar in C++ that can solve this?


Solution

  • There isn't a direct equivalent. You could implement it yourself, the source for AtomicStampedReference is here: https://github.com/JetBrains/jdk8u_jdk/blob/master/src/share/classes/java/util/concurrent/atomic/AtomicStampedReference.java

    You could probably implement this in c++ maybe making use of std::atomic<std::shared_ptr> to implement the private volatile Pair<V> pair.

    If you don't need the full functionality of AtomicStampedReference you can probably use std::atomic<std::shared_ptr> directly in your code. If you don't have c++20 then you can use the previous stand-alone atomic shared_ptr functions