分享方式:


_InterlockedDecrement 內建函式

提供 Win32 Windows SDK InterlockedDecrement 函式的編譯器內建支援。 內部 _InterlockedDecrement 函數是 Microsoft 特有的

語法

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

參數

lpAddend
[in, out]要遞減之變數的揮發性指標。

傳回值

傳回值是所產生的遞減值。

需求

內建 架構
_InterlockedDecrement, _InterlockedDecrement16 x86、ARM、x64、ARM64
_InterlockedDecrement64 ARM、x64、ARM64
_InterlockedDecrement_acq, _InterlockedDecrement_rel, _InterlockedDecrement_nf, _InterlockedDecrement16_acq, _InterlockedDecrement16_rel, _InterlockedDecrement16_nf, _InterlockedDecrement64_acq, _InterlockedDecrement64_rel, _InterlockedDecrement64_nf, ARM、ARM64

標頭檔 < 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);
}

另請參閱

編譯器內建函式
關鍵字
與 x86 編譯器衝突