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
4
votes
1 answer

Understanding cmpxchg8b/cmpxchg16b operation

The SDM text for this instruction has the following block: This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s bus, the destination operand receives a…
4
votes
0 answers

Mutex implementation without hardware support of Test-and-Set and CAS-operations

Let's we have the following simple mutex definition: class Mutex { private: bool lock; public: void acquire(); void release(); Mutex() { lock = 0; } }; And the following acquire() func realization (with using of…
TwITe
  • 400
  • 2
  • 14
4
votes
2 answers

Can CAS fail for all threads?

I'm reading about [lock cmpxchg description]) https://www.felixcloutier.com/x86/CMPXCHG.html): This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s…
4
votes
1 answer

CountingElements solution

I am trying to understand a problem and the solution provided. But not able to grasp it completely https://codility.com/media/train/2-CountingElements.pdf You are given an integer m (1 <= m <= 1 000 000) and two non-empty, zero-indexed arrays A…
Sameer
  • 3,124
  • 5
  • 30
  • 57
4
votes
0 answers

Why does Java 8 provide a LongAccumulator but not an IntAccumulator?

Currently, my program makes heavy use of AtomicIntegers in order to keep track of running maximums of ints that are reported in parallel. After reading about Java 8's new LongAccumulators, I had hoped to use an IntAccumulator with max as the…
4
votes
0 answers

Is __atomic_compare_exchange equivalent to _InterlockedCompareExchange?

The Visual C++ compiler has an intrinsic called _InterlockedCompareExchange, and while looking for a non-Microsoft-specific alternative, I found GCC's __atomic_compare_exchange (formerly known as __sync_val_compare_and_swap) built-in function. As…
Venemo
  • 18,515
  • 13
  • 84
  • 125
4
votes
2 answers

How to atomically set a value if a condition holds in C/C++?

I need to atomically assign a = b if condition c holds, and do not assign if the condition does not hold. Is there a way to do this in C/C++? Clarification: I meant atomically "test and assign", rather than "atomically assign".
Seves
  • 135
  • 2
  • 12
4
votes
1 answer

Compare and swap with and without garbage collector

How does CAS works? How does it work with garbage collector? Where is the problem and how does it work without garbage collector? I was reading a presentation about CAS and using it on "write rarely, read many" problem and there was said, that use…
Jakub Wagner
  • 428
  • 1
  • 5
  • 15
4
votes
1 answer

What' s the advantage of LL/SC when compared with CAS (compare-and-swap)?

What' s the advantage of LL/SC comparing with CAS(compare and swap) in computer architecture? I think LL/SC can case livelock in many-core system, and case ABA problem, but CAS does not. I can not find out any advantage of LL/SC comparing with CAS.…
4
votes
1 answer

C++11 Lock free update 2 variables atomically

I'd like to update atomicX when a thread finds a new minimum to change it to. When it does set the new minimum, I would also like to change a variable y, atomically. Is there a way to do this without locks? Example of a thread function executing on…
David
  • 27,652
  • 18
  • 89
  • 138
4
votes
1 answer

How are condition variables implemented?

This has baffled me for a long time. Given basic atomic primitives like compare & swap, I can see how to implement a spin lock (from which I can build mutexes). However, I don't see how I can build condition variables out of this. How is this done?
anon
  • 41,035
  • 53
  • 197
  • 293
4
votes
2 answers

Why are CAS (Atomic) operations faster than synchronized or volatile operations

From what I understand, synchronized keyword syncs local thread cache with main memory. volatile keyword basically always reads the variable from the main memory at every access. Of course accessing main memory is much more expensive than local…
Salil Surendran
  • 2,245
  • 4
  • 27
  • 42
4
votes
2 answers

Why does GCC pad this bit-field?

Program is in C using std=c99, this is on a 64-bit machine. struct epochs { volatile unsigned int epoch : 1; volatile unsigned int pulse : 1; volatile unsigned int active0 : 7; volatile unsigned int active1 : 7; volatile…
Exodist
  • 629
  • 5
  • 15
4
votes
3 answers

Do atomic CAS-operations on x86_64 and ARM always use std::memory_order_seq_cst?

As Anthony Williams said: some_atomic.load(std::memory_order_acquire) does just drop through to a simple load instruction, and some_atomic.store(std::memory_order_release) drops through to a simple store instruction. It is known that on x86…
Alex
  • 12,578
  • 15
  • 99
  • 195
4
votes
3 answers

how to prevent corruption in concurrent lifo stack implemented with atomic compare and swap

Below is a simplified C program that demonstrates a problem I am having with a concurrent stack implemented using the GNU built in compare and swap on an intel cpu. It took me a while to understand what was happening, but now that I do I see that…
drawnonward
  • 53,459
  • 16
  • 107
  • 112