Partager via


_InterlockedDecrement Intrinsic Functions

Microsoft Specific

Provides compiler intrinsic support for the Win32 Windows SDK InterlockedDecrement function.

long _InterlockedDecrement(
   long * lpAddend
);
long _InterlockedDecrement_acq(
   long * lpAddend
);
long _InterlockedDecrement_acq(
   long * lpAddend
);
short _InterlockedDecrement16(
   short * lpAddend
);
short _InterlockedDecrement16_acq(
   short * lpAddend
);
short _InterlockedDecrement16_rel(
   short * lpAddend
);
__int64 _InterlockedDecrement64(
   __int64 * lpAddend
);
__int64 _InterlockedDecrement64_acq(
   __int64 * lpAddend
);
__int64 _InterlockedDecrement64_rel(
   __int64 * lpAddend
);

Parameters

  • [in, out] lpAddend
    Pointer to the variable to be decremented.

Return Value

Windows 98, Windows NT 4.0, and later: The return value is the resulting decremented value.

Windows 95, Windows NT 3.51, and earlier: If the result of the operation is zero, the return value is zero. If the result of the operation is less than zero, the return value is negative, but it is not necessarily equal to the result. If the result of the operation is greater than zero, the return value is positive, but it is not necessarily equal to the result.

Requirements

Intrinsic

Architecture

_InterlockedDecrement

x86, IPF, x64

_InterlockedDecrement_acq

IPF

_InterlockedDecrement_rel

IPF

_InterlockedDecrement16

x86, IPF, x64

_InterlockedDecrement16_acq

IPF

_InterlockedDecrement16_rel

IPF

_InterlockedDecrement64

IPF, x64

_InterlockedDecrement64_acq

IPF

_InterlockedDecrement64_rel

IPF

Header file <intrin.h>

Remarks

There are several variations on _InterlockedDecrement that vary based on the data types they involve and whether processor-specific acquire or release semantics is used.

While the _InterlockedDecrement function operates on 32-bit integer values, _InterlockedDecrement16 operates on 16-bit integer values and _InterlockedDecrement64 operates on 64-bit integer values.

The _InterlockedDecrement_acq, _InterlockedDecrement16_acq and _InterlockedDecrement64_acq intrinsic functions are the same as the corresponding functions without the _acq suffix except that the operation is performed with acquire semantics, which is useful when entering a critical section.

The _InterlockedDecrement_rel, _InterlockedDecrement16_rel and _InterlockedDecrement64_rel intrinsic function are the same as the corresponding functions without the _rel suffix except that the operation is performed with release semantics, which is useful when leaving a critical section.

The variable pointed to by the lpAddend parameter must be aligned on a 32-bit boundary; otherwise, this function fails on multiprocessor x86 systems and any non-x86 systems.

These functions behave as read-write memory barriers. For more information, see _ReadWriteBarrier.

These routines are only available as intrinsics.

Example

// compiler_intrinsics_interlocked.cpp
// compile with: /Oi
#define _CRT_RAND_S

#include <cstdlib>
#include <cstdio>
#include <process.h>
#include <windows.h>

// To declare an interlocked function for use as an intrinsic,
// include intrin.h and put the function in a #pragma intrinsic 
// statement.
#include <intrin.h>

#pragma intrinsic (_InterlockedIncrement)

// Data to protect with the interlocked functions.
volatile LONG data = 1;

void __cdecl SimpleThread(void* pParam);

const int THREAD_COUNT = 6;

int main() {
   DWORD num;
   HANDLE threads[THREAD_COUNT];
   int args[THREAD_COUNT];
   int i;

   for (i = 0; i < THREAD_COUNT; i++) {
     args[i] = i + 1;
     threads[i] = reinterpret_cast<HANDLE>(_beginthread(SimpleThread, 0, 
                           args + i));
      if (threads[i] == reinterpret_cast<HANDLE>(-1))
         // error creating threads
         break;
   }

   WaitForMultipleObjects(i, threads, true, INFINITE);
}

// Code for our simple thread
void __cdecl SimpleThread(void* pParam) {
   int threadNum = *((int*)pParam);
   int counter;
   unsigned int randomValue;
   unsigned int time;
   errno_t err = rand_s(&randomValue);

   if (err == 0) {
      time = (unsigned int) ((double) randomValue / (double) UINT_MAX * 500);
      while (data < 100) {
         if (data < 100) {
            _InterlockedIncrement(&data);
            printf_s("Thread %d: %d\n", threadNum, data);
         }

         Sleep(time);   // wait up to half of a second
      }
   }

   printf_s("Thread %d complete: %d\n", threadNum, data);
}

See Also

Reference

Compiler Intrinsics

C++ Keywords

Conflicts with the x86 Compiler