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
79
votes
5 answers

Java Concurrency: CAS vs Locking

I'm reading the Book Java Concurrency in Practice. In chapter 15, they are talking about the nonblocking algorithms and the compare-and-swap (CAS) method. It is written that CAS perform much better than the locking methods. I want to ask the people…
Prine
  • 12,192
  • 8
  • 40
  • 59
40
votes
3 answers

Is x86 CMPXCHG atomic, if so why does it need LOCK?

The Intel documentation says This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. My question is Can CMPXCHG operate with memory address? From the document it seems not but can anyone confirm that…
Alex Suo
  • 2,977
  • 1
  • 14
  • 22
39
votes
2 answers

C++11 atomic memory ordering - is this a correct usage of relaxed (release-consume) ordering?

I have recently made a port to C++11 using std::atomic of a triple buffer to be used as a concurrency sync mechanism. The idea behind this thread sync approach is that for a producer-consumer situation where you have a producer that is running…
André Neves
  • 1,066
  • 10
  • 14
35
votes
3 answers

How Compare and Swap works

I have read quite some posts that say compare and swap guarantees atomicity, However I am still not able to get how does it. Here is general pseudo code for compare and swap: int CAS(int *ptr,int oldvalue,int newvalue) { int temp = *ptr; …
StackSmasher
  • 359
  • 1
  • 3
  • 6
34
votes
3 answers

Java compare and swap semantics and performance

What is the semantics of compare and swap in Java? Namely, does the compare and swap method of an AtomicInteger just guarantee ordered access between different threads to the particular memory location of the atomic integer instance, or does it…
axel22
  • 32,045
  • 9
  • 125
  • 137
33
votes
2 answers

How does "Compare And Set" in AtomicInteger works

AtomicInteger works with two concepts : CAS and volatile variable. Using volatile variable insures that the current value will be visible to all threads and it will not be cached. But I am confused over CAS(compare AND set) concept which is…
Onki
  • 1,879
  • 6
  • 38
  • 58
33
votes
3 answers

Is synchronizing with `std::mutex` slower than with `std::atomic(memory_order_seq_cst)`?

The main reason for using atomics over mutexes, is that mutexes are expensive but with the default memory model for atomics being memory_order_seq_cst, isn't this just as expensive? Question: Can concurrent a program using locks be as fast as…
jaybny
  • 1,026
  • 2
  • 13
  • 29
32
votes
2 answers

Why isn't atomic double fully implemented

My question is quite simple. Why isn't std::atomic implemented completely? I know it has to do with atomic RMW (read-modify-write) access. But I really don't see, why this shouldn't be possible on a double. It's specified that any trivially…
laurisvr
  • 2,724
  • 6
  • 25
  • 44
27
votes
4 answers

In Java what is the performance of AtomicInteger compareAndSet() versus synchronized keyword?

I was implementing a FIFO queue of requests instances (preallocated request objects for speed) and started with using the "synchronized" keyword on the add method. The method was quite short (check if room in fixed size buffer, then add value to…
Alan Kent
  • 897
  • 1
  • 8
  • 12
26
votes
2 answers

Haskell: How does 'atomicModifyIORef' work?

Can someone explain how atomicModifyIORef works? In particular: (1) Does it wait for a lock, or optimistically try and retry if there's contention (like TVar). (2) Why is the signature of atomicModifyIORef different to the signature of modifyIORef?…
Clinton
  • 22,361
  • 15
  • 67
  • 163
18
votes
5 answers

What is the difference between Atomic Integer and Normal immutable Integer class in Java?

As Integer class is also immutable class and we know that immutable class is thread-safe what is the need of Atomic Integer. I am confused . Is it the reason that reads and write of immutable objects need not be atomic whereas read and write of…
user18424
  • 401
  • 1
  • 3
  • 8
17
votes
2 answers

Compare and swap C++0x

From the C++0x proposal on C++ Atomic Types and Operations: 29.1 Order and Consistency [atomics.order] Add a new sub-clause with the following paragraphs. The enumeration memory_order specifies the detailed regular (non-atomic) memory…
axel22
  • 32,045
  • 9
  • 125
  • 137
17
votes
3 answers

Atomically increment two integers with CAS

Apparently, it is possible to atomically increment two integers with compare-and-swap instructions. This talk claims that such an algorithm exists but it does not detail what it looks like. How can this be done? (Note, that the obvious solution of…
boot4life
  • 4,966
  • 7
  • 25
  • 47
17
votes
3 answers

atomic_compare_exchange with greater-than instead of equals?

C++11 has a 'compare and exchange' operation for atomic variables. The semantics are: Atomically compares the value pointed to by obj with the value pointed to by expected, and if those are equal, replaces the former with desired (performs…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
15
votes
1 answer

Real-world examples for ABA in multithreading

I'm looking for some nice real-world examples of the ABA-problem causing trouble in multithreaded code. The ABA-problem occurs in concurrent code when executing an atomic compare-and-swap instruction. If a thread is interrupted immediately before…
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
1
2 3
22 23