InterlockedCompareExchangePointer 的作用和例子

PVOID InterlockedCompareExchangePointer(
        [in, out] PVOID volatile *Destination,
        [in] PVOID Exchange,
        [in] PVOID Comperand
);

InterlockedCompareExchangePointer  执行原子操作,该原子操作将目标指向的输入指针值与指针值 Comperand 进行比较。

如果 Comperand 等于 *Destination,则 *Destination 设置为等于 Exchange。 否则,*目标 不变。返回 *Destination (处指针的原始值,即此指针在条目处的值) 

为什么要单独写一个指针版的函数呢,有什么区别呢,是改变指针本身还是指针对应的值,有各变量的函数地址和指针和指针值,写了一个Demo跑跑看

#include <iostream>
#include <Windows.h>

int main()
{
	int a1 = 1;
	std::cout << "&a1=" <<&a1<< ",a1=" << a1 << std::endl;
	int a2 = 2;
	std::cout << "&a2=" << &a2 << ",a2=" << a2 << std::endl;
	int a3 = 3;
	std::cout << "&a3=" << &a3 << ",a3=" << a3 << std::endl;

	int* Oth;
	int* Exc;
	int* Com;
	int* Ret=0;
	std::cout << "=======" << std::endl;
	Oth = &a1;
	std::cout << "&Oth=" << &Oth<< ",Oth=" << Oth <<",*Oth=" << *Oth << std::endl;
	Exc = &a2;
	std::cout << "&Exc=" << &Exc << ",Exc=" << Exc << ",*Exc=" << *Exc << std::endl;
	Com = &a3;
	std::cout << "&Com=" << &Com << ",Com=" << Com << ",*Com=" << *Com << std::endl;
	std::cout << "&Ret=" << &Ret << ",Ret=" << Ret << std::endl;
	std::cout << "=======" << std::endl;
	volatile int* Des = Com;//Oth //Destination value
	std::cout << "Des = Com" << std::endl;
	std::cout << "&Des=" << &Des << ",Des=" << Des << ",*Des=" << *Des << std::endl;	
	std::cout << "Ret = (int*)::InterlockedCompareExchangePointer((volatile PVOID*)&Des, Exc, Com)" << std::endl;
	Ret = (int*)::InterlockedCompareExchangePointer((volatile PVOID*)&Des, Exc, Com);
	std::cout <<  "=======" <<std::endl;
	std::cout << "&a1=" << &a1 << ",a1=" << a1 << std::endl;
	std::cout << "&a2=" << &a2 << ",a2=" << a2 << std::endl;
	std::cout << "&a3=" << &a3 << ",a3=" << a3 << std::endl;
	std::cout << "=======" << std::endl;
	std::cout << "&Oth=" << &Oth << ",Oth=" << Oth << ",*Oth=" << *Oth << std::endl;
	std::cout << "&Des=" << &Des << ",Des=" << Des << ",*Des=" << *Des << std::endl;
	std::cout << "&Exc=" << &Exc << ",Exc=" << Exc << ",*Exc=" << *Exc << std::endl;
	std::cout << "&Com=" << &Com << ",Com=" << Com << ",*Com=" << *Com << std::endl;
	std::cout << "---" << std::endl;
	std::cout << "&Ret=" << &Ret << ",Ret=" << Ret << ",*Ret=" << *Ret << std::endl;
	std::cout << "=======" << std::endl;
}
 

运行结果

&a1=00000076AA0FF604,a1=1
&a2=00000076AA0FF624,a2=2
&a3=00000076AA0FF644,a3=3
=======
&Oth=00000076AA0FF668,Oth=00000076AA0FF604,*Oth=1
&Exc=00000076AA0FF688,Exc=00000076AA0FF624,*Exc=2
&Com=00000076AA0FF6A8,Com=00000076AA0FF644,*Com=3
&Ret=00000076AA0FF6C8,Ret=0000000000000000
=======
Des = Com
&Des=00000076AA0FF6E8,Des=00000076AA0FF644,*Des=3
Ret = (int*)::InterlockedCompareExchangePointer((volatile PVOID*)&Des, Exc, Com)
=======
&a1=00000076AA0FF604,a1=1
&a2=00000076AA0FF624,a2=2
&a3=00000076AA0FF644,a3=3
=======
&Oth=00000076AA0FF668,Oth=00000076AA0FF604,*Oth=1
&Des=00000076AA0FF6E8,Des=00000076AA0FF624,*Des=2
&Exc=00000076AA0FF688,Exc=00000076AA0FF624,*Exc=2
&Com=00000076AA0FF6A8,Com=00000076AA0FF644,*Com=3
---
&Ret=00000076AA0FF6C8,Ret=00000076AA0FF644,*Ret=3
=======

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值