读书笔记——Windows核心编程(8)Interlocked系列函数

先让我们来复习下小学知识

A+B=C//式中A为被加数,B为加数。

A-B=C//式中A为被减数,B为减数。

再让我们来明确一个知识点:返回值为void的Windows函数意味着一定会执行成功。

------------------by wls------------------我是可爱的分割线------------------by wls------------------

必须确保传给这系列函数的变量地址是经过对齐的,否则可能导致失败。

对齐可使用C运行库的_aligned_malloc函数

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);

Interlocked系列函数:

1. 原子操作

2. 执行得极快(无需在用户模式和内核模式间切换)

------------------by wls------------------我是调皮的分割线------------------by wls------------------

InterlockedAdd加法。只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型。

void InterlockedAdd(
  in   UINT dest,//被加数(被减数),结果保存在这里
  in   UINT value,//加数(减数)
  out  UINT original_value//返回的原始值(dest)
);

InterlockedAndand。只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型。

void InterlockedAnd(
  in   UINT dest,//结果保存在这里
  in   UINT value,//输入值
  out  UINT original_value//返回的原始值(dest)
);

InterlockedOror只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型。

<span style="color:#000000;">void InterlockedOr(
  in   UINT dest,
  in   UINT value,
  out  UINT original_value
);</span>

InterlockedXor,xor只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型

void InterlockedXor(
  in   UINT dest,
  in   UINT value,
  out  UINT original_value
);

InterlockedCompareExchange比较成功则替换值。只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型。

void InterlockedCompareExchange(
  in   UINT dest,//被比较值
  in   UINT compare_value,//比较值
  in   UINT value,//比较成功,dest的值被替换为value
  out  UINT original_value//返回的原始值(dest)
);

InterlockedCompareStore,比较(compares the input to the comparison value, atomically)只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型

void InterlockedCompareStore(
  in  UINT dest,//The destination address
  in  UINT compare_value,//The comparison value.
  in  UINT value//The input value.
);

InterlockedExchange交换。只能用于标量类型资源(scalar-typed resources)和共享内存变量,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型。

void InterlockedExchange(
  in   UINT dest,//待替换值
  in   UINT value,//替换值
  out  UINT original_value//返回的原始值(dest)
);

InterlockedMax查找最大值只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型

void InterlockedMax(
  in   UINT dest,
  in   UINT value,
  out  UINT original_value
);

InterlockedMin查找最小值只能用于int或者uint,适用于:1. 共享内存变量类型;2. 资源变量类型;3. 局部变量类型

void InterlockedMin(
  in   UINT dest,
  in   UINT value,
  out  UINT original_value
);

转载请注明出处http://blog.csdn.net/wlsgzl/article/details/17019121

原文地址:https://www.cnblogs.com/wlsandwho/p/4202163.html