Udostępnij za pośrednictwem


_InterlockedAdd Intrinsic Functions

Microsoft Specific

Perform an atomic addition, which ensures that the operation completes successfully when multiple threads have access to a shared variable.

long _InterlockedAdd(
   long volatile * Addend,
   long Value
);
long _InterlockedAdd_acq(
   long volatile * Addend,
   long Value
);
long _InterlockedAdd_rel(
   long volatile * Addend,
   long Value
);
__int64 _InterlockedAdd64(
   __int64 volatile * Addend,
   __int64 Value
);
__int64 _InterlockedAdd64_acq(
   __int64 volatile * Addend,
   __int64 Value
);
__int64 _InterlockedAdd64_rel(
   __int64 volatile * Addend,
   __int64 Value
);

Parameters

  • [in, out] Addend
    Pointer to the integer to be added to; replaced by the result of the addition.

  • [in] Value
    The value to add.

Return Value

Both functions return the result of the addition.

Requirements

Intrinsic

Architecture

_InterlockedAdd

IPF

_InterlockedAdd_acq

IPF

_InterlockedAdd_rel

IPF

_InterlockedAdd64

IPF

_InterlockedAdd64_acq

IPF

_InterlockedAdd64_rel

IPF

Header file <intrin.h>

Remarks

The versions of these functions with the _acq or _rel suffixes perform an interlocked addition following acquire or release semantics. Acquire semantics means that the result of the operation are made visible to all threads and processors before any subsequent memory reads and writes. Acquire is useful when entering a critical section. Release semantics means that all memory reads and writes are forced to be made visible to all threads and processors before the result of the operation is made visible itself. Release is useful when leaving a critical section.

In Visual C++ 2005, these functions behave as read-write memory barriers. For more information, see _ReadWriteBarrier.

These routines are only available as intrinsics.

Example

// interlockedadd.cpp
// Compile with: /Oi /EHsc
// processor: IPF
#include <stdio.h>
#include <intrin.h>

#pragma intrinsic(_InterlockedAdd)

int main()
{
        long data1 = 0xFF00FF00;
        long data2 = 0x00FF0000;
        long retval;
        retval = _InterlockedAdd(&data1, data2);
        printf("0x%x 0x%x 0x%x", data1, data2, retval);
}

// interlockedadd64.cpp
// compile with: /Oi /EHsc
// processor: IPF
#include <iostream>
#include <intrin.h>
using namespace std;

#pragma intrinsic(_InterlockedAdd64)

int main()
{
        __int64 data1 = 0x0000FF0000000000;
        __int64 data2 = 0x00FF0000FFFFFFFF;
        __int64 retval;
        cout << hex << data1 << " + " << data2 << " = " ;
        retval = _InterlockedAdd64(&data1, data2);
        cout << data1 << endl;
        cout << "Return value: " << retval << endl;
}

Output

0xffffff00 0xff0000 0xffffff00

Output

ff0000000000 + ff0000ffffffff = ffff00ffffffff
Return value: ffff00ffffffff

See Also

Reference

Compiler Intrinsics

Conflicts with the x86 Compiler