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

How to doatomic CAS operation on two adjacent bits of a byte?

Say there is: unsigned char byte = someValue; What I need, is to perform an atomic CAS (compare and swap) operation on some two adjacent bits in byte, say the fourth and the fifth. For sure, there is no way to address something shorter than a byte,…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
0
votes
2 answers

Generating CMPXCHG (without LOCK) in 64-bit builds using Visual C++ (2010)

I need CAS functions to use in a context of multiple threads running on the same CPU (assume that all threads are statically glued to selected CPU, via SetThreadAffinityMask). InterlockedCompareExchange generates LOCK CMPXCHG. The LOCK part comes…
Angstrom
  • 352
  • 1
  • 2
  • 16
0
votes
1 answer

Change InactiveDt to be same date as the next lowest EffectiveDt from the product id

I have a table: CREATE TABLE ProductPrice ( ProductId INT NOT NULL, EffectiveDt DATE NOT NULL, InactiveDt DATE NOT NULL, Price DECIMAL(10,2) NOT NULL ) What I'm wanting to do is use an…
Danny Calladine
  • 321
  • 1
  • 2
  • 13
0
votes
2 answers

Lock free list with sync_val_compare_and_swap

I'm trying to provide a thread safe way to update an object. I've seen a few posts about it, but nothing that answers my question 100%. What I want to do is maintain a pointer called 'm_First' to point at my data. If someone calls get, I deference…
0
votes
1 answer

MinGW undefined reference to `___atomic_fetch_sub_4'

When trying to build a simple test program that uses atomic operations, I get the error undefined reference to `___atomic_fetch_sub_4' Specifically, it only happens when I do a -- combined with ==: std::atomic<std::size_t> foo; if (--foo == 42) {…
Mark
  • 2,082
  • 3
  • 17
  • 30
-1
votes
1 answer

Can we use Compare-and-Swap operation on non-atomic variable?

Hi I noticed that the CAS Compare-and-Swap operations are usually operated on atomic variables, see https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange, …
Dachuan Huang
  • 115
  • 1
  • 8
-1
votes
3 answers

How can I check for null at a specific index of a String Array to change its value?

I am trying to print an array of randomly generated sets of three numbers. Sometimes the sets include a null value when they are printed to the console. Curiously, it is only and always, two of the sixty-six randomly produce sets of three digits…
-1
votes
2 answers

Custom compare function for std::sort() C++

Many times I have to use a custom compare function for sorting. I can Implement it but Sometimes I make a mistake. I usually get confused if it returns true then it will be swapped or not? Can someone explain what happens if it returns true and what…
anon
-1
votes
1 answer

If compareAndSet fails, is the code below still executed?

I have a very dumb question. If I use AtomicReferences compareAndSet this way original.set(atomic.get()); long next = some new value atomic.compareAndSet(original.get(), next); ....more code.... is more code still updated if the…
Olli
  • 906
  • 10
  • 25
-2
votes
1 answer

Indirect atomic swap for struct with pointer

I want to atomically swap two big memory locations defined as the struct VValue. Since the size of the struct is bigger that long, there is no architectural support to directly compare and swap the two structs. However, one way is to swap the…
Branky
  • 373
  • 8
  • 23
-2
votes
2 answers

Need Optimized Python Solution for Leetcode 3Sum Question

Here's the Problem Statement: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate…
Parth S.
  • 63
  • 1
  • 9
-2
votes
2 answers

Atomic CAS with non-volatile field

If we consider for a moment the following class and x86 architecture: class Foo{ int x; void increment() { compareAndSwap(x, x+1); } int getX() { return x; } } If we now assume one thread is calling increment, and the…
Bober02
  • 15,034
  • 31
  • 92
  • 178
-2
votes
2 answers

I heard that INC instruction is not atomic. Then, how CAS instruction can be atomic?

CAS is very primitive lock free technique, and I know that it is atomic. Also, it is much more complex operation than INC. It should compare value and if value is not changed, CAS sets the new value while guaranteeing that other thread does not…
Hozard
  • 193
  • 2
  • 9
-3
votes
1 answer

What is the meaning of the spin in the getAndIncrement method in the AtomicInteger class?

In the getAndIncrement method of the AtomicInteger class,the getAndAddInt method of the unsafe class is called,which contains the compareAndSwapInt method and the spin(do...while). The compareAndSwapInt method is thread-safe,so Why not just change…
1 2 3
22
23