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
14
votes
4 answers

CAS vs synchronized performance

I've had this question for quite a while now, trying to read lots of resources and understanding what is going on - but I've still failed to get a good understanding of why things are the way they are. Simply put I'm trying to test how a CAS would…
Eugene
  • 117,005
  • 15
  • 201
  • 306
14
votes
2 answers

Why do C++11 CAS operations take two pointer parameters?

Many of the C++11 CAS operations (e.g., atomic_compare_exchange_weak, atomic_compare_exchange_strong) take two pointers and a value, i.e., like this: bool atomic_compare_exchange(T* pointer, T* expected, // pseudodeclaration! …
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
13
votes
3 answers

weakCompareAndSwap vs compareAndSwap

This question is not about the difference between them - I know what spurious failure is and why it happens on LL/SC. My question is if I'm on intel x86 and using java-9 (build 149), why is there a difference between their assembly code? public…
Eugene
  • 117,005
  • 15
  • 201
  • 306
13
votes
5 answers

Why do we need to compareAndSet when set is already atomic in Java?

Since Atomic means thread safe. When do we ever use compareAndSet when .set() itself is Atomic and thread safe in java? say for example I want to set a variable atomically such that every other thread can see it (but I want the variable to be set in…
user1870400
  • 6,028
  • 13
  • 54
  • 115
13
votes
3 answers

What is Compare And Swap good for?

I was recently reading about the Compare And Swap atomic action (CMPXCHG, .NET's Interlocked.CompareExchange, whatever). I understand how it works internally, and how it's used from a client. What I can't quite figure out is when would someone use…
12
votes
1 answer

When should std::atomic_compare_exchange_strong be used?

There are two atomic CAS operations in C++11: atomic_compare_exchange_weak and atomic_compare_exchange_strong. According to cppreference: The weak forms of the functions are allowed to fail spuriously, that is, act as if *obj != *expected even if…
user571470
  • 394
  • 4
  • 14
11
votes
2 answers

compare-and-swap atomic operation vs Load-link/store-conditional operation

Under an x86 processor I am not sure of the difference between compare-and-swap atomic operation and Load-link/store-conditional operation. Is the latter safer than the former? Is it the case that the first is better than the second?
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
11
votes
5 answers

Why is CompareAndSwap instruction considered expensive?

Why is CompareAndSwap instruction considered expensive? I read in a book: "Memory barriers are expensive, about as expensive as an atomic compareAndSet() instruction." Thanks!
10
votes
2 answers

Should interlocked implementations based on CompareExchange use SpinWait?

Below is an implementation of an interlocked method based on Interlocked.CompareExchange. Is it advisable for this code to use a SpinWait spin before reiterating? public static bool AddIfLessThan(ref int location, int value, int comparison) { …
Timo
  • 7,992
  • 4
  • 49
  • 67
10
votes
5 answers

Compare and swap in machine code in C

How would you write a function in C which does an atomic compare and swap on an integer value, using embedded machine code (assuming, say, x86 architecture)? Can it be any more specific if its written only for the i7 processor? Does the translation…
axel22
  • 32,045
  • 9
  • 125
  • 137
10
votes
1 answer

Locks vs Compare-and-swap

I've been reading about lock-free techniques, like Compare-and-swap and leveraging the Interlocked and SpinWait classes to achieve thread synchronization without locking. I've ran a few tests of my own, where I simply have many threads trying to…
dcastro
  • 66,540
  • 21
  • 145
  • 155
10
votes
2 answers

How does wait / notify work at the JVM level?

Wait and notify seem like messages that are passed between threads, if this is true there must be queues for buffering these messages. If so then there must be atomic operations for adding messages to and removing message from the queue, also there…
9
votes
3 answers

How to: Write a thread-safe method that may only be called once?

I'm attempting to write a thread-safe method which may only be called once (per object instance). An exception should be thrown if it has been called before. I have come up with two solutions. Are they both correct? If not, what's wrong with…
9
votes
5 answers

CMPXCHG16B correct?

This doesn't exactly seem to be right although I am unsure why. Advice would be great as the documentation for CMPXCHG16B is pretty minimal (I don't own any intel manuals...) template<> inline bool cas(volatile types::uint128_t *src,…
JH.
  • 4,147
  • 3
  • 19
  • 20
9
votes
2 answers

Compare-and-Swap over POSIX-compliant filesystem objects

There are several operations which POSIX-compliant operating systems can do atomically with filesystem objects (files and folders). Here is a list of such presumably atomic operations: rename or move file or folder create hardlink create…
Volodymyr Frolov
  • 1,256
  • 5
  • 16
  • 25
1
2
3
22 23