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
9
votes
2 answers

Java Atomic Variable set() vs compareAndSet()

I want to know the difference between set() and compareAndSet() in atomic classes. Does the set() method also ensure the atomic process? For example this code: public class sampleAtomic{ private static AtomicLong id = new AtomicLong(0); …
Chrisma Andhika
  • 321
  • 2
  • 9
  • 19
9
votes
2 answers

Atomic Compare And Swap with struct in Go

I am trying to create a non-blocking queue package for concurrent application using the algorithm by Maged M. Michael and Michael L. Scott as described here. This requires the use of atomic CompareAndSwap which is offered by the "sync/atomic"…
ANisus
  • 74,460
  • 29
  • 162
  • 158
8
votes
2 answers

Fetch-and-add using OpenMP atomic operations

I’m using OpenMP and need to use the fetch-and-add operation. However, OpenMP doesn’t provide an appropriate directive/call. I’d like to preserve maximum portability, hence I don’t want to rely on compiler intrinsics. Rather, I’m searching for a way…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
8
votes
3 answers

In what situations can do-while be more efficient than while?

While vs. do-while While and do-while are functionally equivalent when the blocks are empty, although while seems more natural: do {} while (keepLooping()); while (keepLooping()) {} One typical use case of a while/do-while with an empty block is to…
assylias
  • 321,522
  • 82
  • 660
  • 783
8
votes
2 answers

Is it guaranteed that compareAndSwap can NOT fail for ALL the participating threads?

Suppose some "N" number of threads are trying to CAS an AtomicInteger variable, Is it guaranteed that the CAS must succeed for exactly ONE thread? Is there a possibility that all the "N" threads fail in an attempt?
javapk
  • 81
  • 2
8
votes
2 answers

atomic compare(not equal) and swap

I want to use atomic compare and swap, but instead of equal to, I want to swap only if the memory location is not equal to the old value. Is it possible in C?
0xhacker
  • 1,091
  • 2
  • 14
  • 26
7
votes
5 answers

Why Double-Checked Locking is used at all?

I keep on running across code that uses double-checked locking, and I'm still confused as to why it's used at all. I initially didn't know that double-checked locking is broken, and when I learned it, it magnified this question for me: why do people…
user541686
  • 205,094
  • 128
  • 528
  • 886
7
votes
1 answer

Is reading a 64-bit atomic value safe on 64-bit platforms if I write/swap using OS atomic functions with barrier?

The question is about the latest iOS and macOS. Suppose I have the following implementation for an atomic Int64 in Swift: struct AtomicInt64 { private var _value: Int64 = 0 init(_ value: Int64) { set(value) } mutating func…
mojuba
  • 11,842
  • 9
  • 51
  • 72
7
votes
2 answers

Interlocked.Exchange slower than Interlocked.CompareExchange?

I came across some odd performance results when optimizing a program, which are shown in the following BenchmarkDotNet benchmark: string _s, _y = "yo"; [Benchmark] public void Exchange() => Interlocked.Exchange(ref _s, null); [Benchmark] public…
Shay Rojansky
  • 15,357
  • 2
  • 40
  • 69
7
votes
1 answer

Compare and Swap on x86 - why is it a full barrier?

As per this question's answer, it seems that LOCK CMPXCHG on x86 actually causes a full barrier. Presumably, this is what Unsafe.compareAndSwapInt() generates under the hood as well. I am struggling to see why that is the case: with MESI protocol,…
Bober02
  • 15,034
  • 31
  • 92
  • 178
7
votes
1 answer

Mixing lightweight transactions and normal writes in Cassandra

The Datastax documentation for lightweight transaction states: "Lightweight transactions use a timestamping mechanism different than for normal operations and mixing LWTs and normal operations can result in errors. If lightweight transactions are…
7
votes
2 answers

Can competing atomic operations starve one another?

Imagine a program with two threads. They are running the following code (CAS refers to Compare and Swap): // Visible to both threads static int test; // Run by thread A void foo() { // Check if value is 'test' and swap in 0xdeadbeef …
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
7
votes
1 answer

Does atomic_cmpxchg() imply memory barriers?

The following two citations seem contradicting: https://www.kernel.org/doc/Documentation/atomic_ops.txt int atomic_cmpxchg(atomic_t *v, int old, int new); This performs an atomic compare exchange operation on the atomic value v, with the given…
7
votes
6 answers

java - stealing bits from references

How do I steal 2 MSBs from an address to do an atomic operation? I'm trying to do a single word CAS An example public class Node { long key; long value; Node lchild; // format is flag1,flag2,address Node rchild; // format is…
7
votes
3 answers

Why Atomic versions are missing for some primitive types while being present for some?

Java provides AtomicInteger,AtomicLong etc which basically compiles to CAS instructions at hardware level. But why such AtomicXXX classes do not exist for other primitive types like short and floating point numbers like float and double?
Geek
  • 26,489
  • 43
  • 149
  • 227
1 2
3
22 23