Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic Compare Operator (No swap)

I am looking for a comparator operator that can be used to compare two atomic variable atomically under C++11. Here i do not want to swap values stored under these atomic objs so i am not interested in compare_and_swap functions. Please refer example below:

std::atomic<uint32_t> readIdx{0};
std::atomic<uint32_t> writeIdx{0};

while(writeIdx + 1 == readIdx)   <<<<------------------
{
     std::this_thread::yield();
}

All i want, to make code represented with arrow line to be atomic. Is it possible? If not, does writeIdx == readIdx is an atomic operation?

like image 411
Manish Shukla Avatar asked Nov 11 '13 06:11

Manish Shukla


People also ask

Is compare and swap atomic?

In computer science, compare-and-swap (CAS) is an atomic instruction used in multithreading to achieve synchronization. It compares the contents of a memory location with a given value and, only if they are the same, modifies the contents of that memory location to a new given value.

When to use compare and swap?

Compare and swap is a technique used when designing concurrent algorithms. Basically, compare and swap compares an expected value to the concrete value of a variable, and if the concrete value of the variable is equals to the expected value, swaps the value of the variable for a new variable.

Is std exchange atomic?

std::atomic<T>::exchangeAtomically replaces the underlying value with desired . The operation is read-modify-write operation. Memory is affected according to the value of order .

What is atomic operation c++?

(C++11) [edit] The atomic library provides components for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object. Atomic objects are free of data races.


1 Answers

It is not possible, since it would not make any sense.

Your code will obtain valid values for the comparison, but it gives very little guarantees on when those values are obtained. So if the check succeeds, all you'll know is that readIdx was at some point in time equal to a value that writeIdx + 1 yielded at some point in time. These two points in time are mostly unrelated. In particular, it is allowed that at no single point in time the value of readIdx was equal to the value of writeIdx + 1 but still the check succeeds.

Here's why this is not really a problem: You won't be able to establish the concept of both atomics being equal at the same time without introducing an additional lock. The problem is that whatever code depends on that condition needs to be part of the same atomic block of execution that performs the check. If it is not, the condition might change before the code finishes executing.

On the other hand, if no part of the code depends on the condition, it makes no sense to introduce it as a concept in the first place.

So here's how to continue: Go back and reevaluate whether you really have code that depends on the condition that both variables have to have the expected values at the time that code executes. If that is a case, you'll need to protect that code with a lock. If not, it is likely that you don't need to check the condition at all, as the guarantees given by your current code are probably too weak to be of any real use.

like image 68
ComicSansMS Avatar answered Sep 28 '22 07:09

ComicSansMS