_InterlockedDecrement 内部函数

Microsoft 专用

为 Win32 Windows SDK InterlockedDecrement 函数提供编译器内部支持。

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

参数

  • [in, out] lpAddend
    指向要递减的变量的指针。

返回值

返回值是生成的递减值。

要求

内部函数

体系结构

_InterlockedDecrement, _InterlockedDecrement16, _InterlockedDecrement64

x86、ARM、x64

_InterlockedDecrement_acq, _InterlockedDecrement_rel, _InterlockedDecrement_nf, _InterlockedDecrement16_acq, _InterlockedDecrement16_rel, _InterlockedDecrement16_nf, _InterlockedDecrement64_acq, _InterlockedDecrement64_rel, _InterlockedDecrement64_nf,

ARM

头文件 <intrin.h>

备注

_InterlockedDecrement 存在几种变体,这些变体根据其涉及的数据类型和是否使用特定于处理器获取或发布语义而有所不同。

_InterlockedDecrement 函数对 32 位整数值操作时,_InterlockedDecrement16 对 16 位整数值操作且 _InterlockedDecrement64 可以对 64 位整数值操作。

ARM 平台上,如果需要(例如在临界区的起点和终点)获取和发布语义,可以使用带 _acq_rel 后缀的函数。 带 _nf(“无围墙”)后缀的内部函数不能充当内存屏障。

由 lpAddend 参数指向的变量必须与 32 位边界对齐;否则,此函数在多处理器 x86 系统和任何非 x86 系统上将失效。 有关更多信息,请参阅对齐

这些例程只能用作内部函数。

示例

// 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);
}

请参见

参考

编译器内部函数

C++ 关键字

与 x86 编译器冲突