For Microsoft's MSVC compiler there are the interlocked functions, as summarized from the MSDN documentation, starting with the adding features:
|
Interlocked function |
Description |
|
InterlockedAdd |
Performs an atomic addition operation on the specified LONG values. |
|
InterlockedAddAcquire |
Performs an atomic addition operation on the specified LONG values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedAddRelease |
Performs an atomic addition operation on the specified LONG values. The operation is performed with release memory ordering semantics. |
|
InterlockedAddNoFence |
Performs an atomic addition operation on the specified LONG values. The operation is performed atomically, but without using memory barriers (covered in this chapter). |
These are the 32-bit versions of this feature. There are also 64-bit versions of this and other methods in the API. Atomic functions tend to be focused on a specific variable type, but variations in this API have been left out of this summary to keep it brief.
We can also see the acquire and release variations. These provide the guarantee that the respective read or write access will be protected from memory reordering (on a hardware level) with any subsequent read or write operation. Finally, the no fence variation (also known as a memory barrier) performs the operation without the use of any memory barriers.
Normally CPUs perform instructions (including memory reads and writes) out of order to optimize performance. Since this type of behavior is not always desirable, memory barriers were added to prevent this instruction reordering.
Next is the atomic AND feature:
|
Interlocked function |
Description |
|
InterlockedAnd |
Performs an atomic AND operation on the specified LONG values. |
|
InterlockedAndAcquire |
Performs an atomic AND operation on the specified LONG values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedAndRelease |
Performs an atomic AND operation on the specified LONG values. The operation is performed with release memory ordering semantics. |
|
InterlockedAndNoFence |
Performs an atomic AND operation on the specified LONG values. The operation is performed atomically, but without using memory barriers. |
The bit-test features are as follows:
|
Interlocked function |
Description |
|
InterlockedBitTestAndComplement |
Tests the specified bit of the specified LONG value and complements it. |
|
InterlockedBitTestAndResetAcquire |
Tests the specified bit of the specified LONG value and sets it to 0. The operation is atomic, and it is performed with acquire memory ordering semantics. |
|
InterlockedBitTestAndResetRelease |
Tests the specified bit of the specified LONG value and sets it to 0. The operation is atomic, and it is performed using memory release semantics. |
|
InterlockedBitTestAndSetAcquire |
Tests the specified bit of the specified LONG value and sets it to 1. The operation is atomic, and it is performed with acquire memory ordering semantics. |
|
InterlockedBitTestAndSetRelease |
Tests the specified bit of the specified LONG value and sets it to 1. The operation is atomic, and it is performed with release memory ordering semantics. |
|
InterlockedBitTestAndReset |
Tests the specified bit of the specified LONG value and sets it to 0. |
|
InterlockedBitTestAndSet |
Tests the specified bit of the specified LONG value and sets it to 1. |
The comparison features can be listed as shown:
|
Interlocked function |
Description |
|
InterlockedCompareExchange |
Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison. |
|
InterlockedCompareExchangeAcquire |
Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison. The operation is performed with acquire memory ordering semantics. |
|
InterlockedCompareExchangeRelease |
Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison. The exchange is performed with release memory ordering semantics. |
|
InterlockedCompareExchangeNoFence |
Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison. The operation is performed atomically, but without using memory barriers. |
|
InterlockedCompareExchangePointer |
Performs an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. |
|
InterlockedCompareExchangePointerAcquire |
Performs an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. The operation is performed with acquire memory ordering semantics. |
|
InterlockedCompareExchangePointerRelease |
Performs an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. The operation is performed with release memory ordering semantics. |
|
InterlockedCompareExchangePointerNoFence |
Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. The operation is performed atomically, but without using memory barriers |
The decrement features are:
|
Interlocked function |
Description |
|
InterlockedDecrement |
Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. |
|
InterlockedDecrementAcquire |
Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with acquire memory ordering semantics. |
|
InterlockedDecrementRelease |
Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with release memory ordering semantics. |
|
InterlockedDecrementNoFence |
Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed atomically, but without using memory barriers. |
The exchange (swap) features are:
|
Interlocked function |
Description |
|
InterlockedExchange |
Sets a 32-bit variable to the specified value as an atomic operation. |
|
InterlockedExchangeAcquire |
Sets a 32-bit variable to the specified value as an atomic operation. The operation is performed with acquire memory ordering semantics. |
|
InterlockedExchangeNoFence |
Sets a 32-bit variable to the specified value as an atomic operation. The operation is performed atomically, but without using memory barriers. |
|
InterlockedExchangePointer |
Atomically exchanges a pair of pointer values. |
|
InterlockedExchangePointerAcquire |
Atomically exchanges a pair of pointer values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedExchangePointerNoFence |
Atomically exchanges a pair of addresses. The operation is performed atomically, but without using memory barriers. |
|
InterlockedExchangeSubtract |
Performs an atomic subtraction of two values. |
|
InterlockedExchangeAdd |
Performs an atomic addition of two 32-bit values. |
|
InterlockedExchangeAddAcquire |
Performs an atomic addition of two 32-bit values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedExchangeAddRelease |
Performs an atomic addition of two 32-bit values. The operation is performed with release memory ordering semantics. |
|
InterlockedExchangeAddNoFence |
Performs an atomic addition of two 32-bit values. The operation is performed atomically, but without using memory barriers. |
The increment features are:
|
Interlocked function |
Description |
|
InterlockedIncrement |
Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. |
|
InterlockedIncrementAcquire |
Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using acquire memory ordering semantics. |
|
InterlockedIncrementRelease |
Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using release memory ordering semantics. |
|
InterlockedIncrementNoFence |
Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed atomically, but without using memory barriers. |
The OR feature:
|
Interlocked function |
Description |
|
InterlockedOr |
Performs an atomic OR operation on the specified LONG values. |
|
InterlockedOrAcquire |
Performs an atomic OR operation on the specified LONG values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedOrRelease |
Performs an atomic OR operation on the specified LONG values. The operation is performed with release memory ordering semantics. |
|
InterlockedOrNoFence |
Performs an atomic OR operation on the specified LONG values. The operation is performed atomically, but without using memory barriers. |
Finally, the exclusive OR (XOR) features are:
|
Interlocked function |
Description |
|
InterlockedXor |
Performs an atomic XOR operation on the specified LONG values. |
|
InterlockedXorAcquire |
Performs an atomic XOR operation on the specified LONG values. The operation is performed with acquire memory ordering semantics. |
|
InterlockedXorRelease |
Performs an atomic XOR operation on the specified LONG values. The operation is performed with release memory ordering semantics. |
|
InterlockedXorNoFence |
Performs an atomic XOR operation on the specified LONG values. The operation is performed atomically, but without using memory barriers. |