Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions for performing atomic operations

Tags:

c

atomic

Are there functions for performing atomic operations (like increment / decrement of an integer) etc supported by C Run time library or any other utility libraries?

If yes, what all operations can be made atomic using such functions?

Will it be more beneficial to use such functions than the normal synchronization primitives like mutex etc?

OS : Windows, Linux, Solaris & VxWorks

like image 911
Jay Avatar asked Feb 20 '10 15:02

Jay


People also ask

What is atomic function in operating system?

Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.

What is an atomic operation example?

An example of atomic operation is instruction execution, usually an instruction feed to the execution unit can't be stopped in the middle. Yet, a statement in high level language results in multiple instructions. It is the root cause of non-atomic operations.

How are atomic operations implemented?

On the majority of modern CPUs, an atomic operation works by locking the affected memory address in the CPU's cache. The CPU acquires the memory address exclusively in its cache and then does not permit any other CPU to acquire or share that address until the operation completes.

What are atomic operations in Python?

An atomic operation is one or a sequence of code instructions that are completed without interruption. A program may be interrupted for one of many reasons. In concurrent programming, a program may be interrupted via a context switch. You may recall that the operating system controls what threads execute and when.


1 Answers

Prior to C11

The C library doesn't have any.

On Linux, gcc provides some -- look for __sync_fetch_and_add, __sync_fetch_and_sub, and so on.

In the case of Windows, look for InterlockedIncrement, InterlockedDecrement``, InterlockedExchange`, and so on. If you use gcc on Windows, I'd guess it also has the same built-ins as it does on Linux (though I haven't verified that).

On Solaris, it'll depend. Presumably if you use gcc, it'll probably (again) have the same built-ins it does under Linux. Otherwise, there are libraries floating around, but nothing really standardized.

C11

C11 added a (reasonably) complete set of atomic operations and atomic types. The operations include things like atomic_fetch_add and atomic_fetch_sum (and *_explicit versions of same that let you specify the ordering model you need, where the default ones always use memory_order_seq_cst). There are also fence functions, such as atomic_thread_fence and atomic_signal_fence.

The types correspond to each of the normal integer types--for example, atomic_int8_t corresponding to int8_t and atomic_uint_least64_t corrsponding to uint_least64_t. Those are typedef names defined in <stdatomic.h>. To avoid conflicts with any existing names, you can omit the header; the compiler itself uses names in the implementor's namespace (e.g., _Atomic_uint_least32_t instead of atomic_uint_least32_t).

like image 157
Jerry Coffin Avatar answered Oct 02 '22 14:10

Jerry Coffin