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

410 Views Asked by At

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?

2

There are 2 best solutions below

1
Alan Birtles On BEST ANSWER

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

4
realestLink On

Maybe you should look at std::atomic. I have never heard of "AtomicStampedReference", but from just a cursory look, it seems to be an atomic reference. std::atomic is for atomic variables. Hopefully this is what you're looking for.