3.0 Atomic Functions

This section alphabetically lists the atomic functions from stdlib.h and describes their purpose, syntax, parameters, and return values.

An atomic function is guaranteed to complete its memory operation before any other processor or bus agent can access the same memory. The Intel 386, Intel 486, Pentium, and Pentium Pro family processors guarantee that the following memory operations are always carried out atomically:

The following atomic functions perform atomically on memory locations with any alignment. These functions alleviate the use of other synchronization mechanisms such as mutexes or spin locks, which are otherwise required to guarantee an atomic operation.

For example, in a multiprocessor environment you should use atomic functions to change the value of a global variable that is not protected by a synchronization primitive and that is accessible by multiple threads. For instance, if you use a C increment operator (a++) to increase the value of such a global variable, you have no guarantee that the operation will complete before another thread accesses the variable—a compiler can generate multiple machine language instructions for a++. However, the atomic_inc function guarantees the increment operation to complete before any other thread can access the data.

The atomic functions are needed only when multiple threads have access to the same memory location and you have not protected the access with a mutex or some other synchronization device.

This section describes the following functions: