Questions tagged [compare-and-swap]

Compare and swap (compare and exchange) is an atomic operation that writes a value to a memory location only if its current value is equal to a given expected value.

Compare and swap (compare and exchange) is an atomic operation that writes a value to a memory location only if its current value is equal to a given expected value. CAS operations is often used in multithreading to achieve synchronization.

346 questions
5
votes
0 answers

The reason behind back-off strategy for spin lock

I'm looking at the spin lock implementation in JVM HotSpot from OpenJDK12. Here is how it is implemented (comments preserved): // Polite TATAS spinlock with exponential backoff - bounded spin. // Ideally we'd use processor cycles, time or vtime to…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
5
votes
1 answer

Consensus Value

while reading on concurrent programming, I came across the term Consensus Number in Compare-And-Swap & Compare-And-Set operations. I'm having trouble in understanding what is meant by this term, can anyone explain?? Thank You!!
5
votes
1 answer

Why isn't CAS (Compare And Swap) equivalent to busy wait loops?

Reading a bit about lock free programming in the past few days I came across the util.java.Random class creating it's bits using the following routine: protected int next(int bits) { long oldseed, nextseed; AtomicLong seed = this.seed; …
Kilian
  • 1,540
  • 16
  • 28
5
votes
1 answer

memory_order_relaxed and Atomic RMW operations

The C++ Standard says that RMW (Read-Modify-Write) operations on atomics will operate on the latest value of the atomic variable. Consequently using memory_order_relaxed with these operations won't affect the RMW operation when executed concurrently…
MS Srikkanth
  • 3,829
  • 3
  • 19
  • 33
5
votes
2 answers

AtomicInteger.compareAndSet that returns the original value, not a boolean

Java's AtomicInteger offers public final boolean compareAndSet(int expect, int update). If false is returned, I would like to know what the value actually was at the time when the comparison failed. Is this possible in Java? In .Net, there's public…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
5
votes
2 answers

compareAndSet memory effects of unsuccessful operations

Java exposes the CAS operation through its atomic classes, e.g. boolean compareAndSet(expected,update) The JavaDocs specifies the memory effects of a compareAndSet operation as follows: compareAndSet and all other read-and-update operations …
Dominik
  • 1,058
  • 8
  • 20
5
votes
1 answer

Don't understand CAS operations in Java atomic package

I am studying the concurrent packages in a Java book. I don't quite understand something the book says about CAS operations. The following code sample is a thread-safe counter class from the book. public class Counter{ private AtomicInteger…
driftwood
  • 2,051
  • 4
  • 21
  • 28
5
votes
2 answers

Implement CompareAndSwap for bool

In .NET framework, atomic operation CompareAndExchange is only defined for int, long, double and reference type. But I need CompareAndExchange for bool type. How can I implement CompareAndSwap for bool?
user
  • 5,335
  • 7
  • 47
  • 63
5
votes
1 answer

How can std::atomic_compare_exchange_* etc. be used with arbitrary pointers?

InterlockedCompareExchange in Windows, as well as __sync_val_compare_and_swap in gcc take pointers, and so I can pass in any address e.g. pointing into a shared memory block into those functions. For non-x86 architectures, I might have to ensure…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
5
votes
1 answer

Should std::atomic::load be doing a compare-and-swap loop?

Summary: I had expected that std::atomic::load with std::memory_order_relaxed would be close to the performance of just loading a pointer directly, at least when the loaded value rarely changes. I saw far worse performance for the atomic load…
Bjarke H. Roune
  • 3,667
  • 2
  • 22
  • 26
4
votes
1 answer

Lockless increment if greater than

Is anybody aware of a lockless way to perform what is logically equivalent to a compare_and_swap_if_greater_than()? We have compare_and_swap(), which is really compare_and_swap_if_equal(). The best I have right now is to use a spin mutex, but I…
tgoodhart
  • 3,111
  • 26
  • 37
4
votes
2 answers

Can anyone interpret this C++ code (from OpenJDK6) into plain English?

Here's a code snippet from OpenJDK6's hotspot/src/share/vm/prims/unsafe.cpp (starting on line 1082): // JSR166 ------------------------------------------------------------------ UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env,…
Clark Bao
  • 1,743
  • 3
  • 21
  • 39
4
votes
0 answers

c++ - Flaky atomic lock.. but why?

Question solved: see the edit. x86_64, Linux, gcc 11.1, gnu++20. Before using const std::lock_guard lock( mut ); I wanted to give a go to my own implementation of multithreading lock, to have a better understanding of the whole thing. The…
Kroma
  • 1,109
  • 9
  • 18
4
votes
2 answers

Don't really get the logic of std::atomic::compare_exchange_weak and compare_exchange_strong

I've read https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange Atomically compares the object representation (until C++20)value representation (since C++20) of *this with that of expected, and if those are bitwise-equal, replaces the…
Bruice
  • 543
  • 3
  • 11
4
votes
1 answer

CMPXCHG16B and MSVC Implementation of not the default?

It seems that for CMPXCHG16B to be used, one has to define _STD_ATOMIC_ALWAYS_USE_CMPXCHG16B = 1 so that these instructions are used. Why is this the default? I would have never found out about this unless I read the whole atomic.h header…
J. Tully
  • 129
  • 1
  • 9