Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any atomic set operation that returns the old value in C?

Tags:

c

atomic

I am looking for some atomic_set-like function that sets the variable's value atomically and, at the same time, it returns the previous value similar to the compare_and_swap.

Here is what I expect:

int old_val = atomic_set(mem_address, 10);
like image 518
campescassiano Avatar asked May 14 '18 06:05

campescassiano


1 Answers

The C11 <stdatomic.h> defines atomic_exchange() and atomic_exchange_explicit() that perform this operation.

The atomic_exchange_explicit() form allows you to specify the memory ordering required (the plain atomic_exchange() uses the strongest memory ordering memory_order_seq_cst).

like image 74
caf Avatar answered Oct 24 '22 01:10

caf