InterlockedCompareExchangePointer


函数作用

The InterlockedCompareExchangePointer routine performs an atomic operation that compares the input pointer value pointed to by Destination with the pointer value Comperand.

  • 适用场景:多线程中,对指针进行赋值操作
  • 用于比较两个指针的值,而不是指针指向的值,交换的也是两个指针的值
  • 原子化的比较和赋值函数,比较dest指针和comparand指针,如果dest等于comperand,则把dest的值赋为exchange
  • 函数可以操作3个变量
    • dest,就是将要对其进行操作的变量
    • exchange,在比较后需要赋值给dest的变量
    • comperand,与dest比较的的变量

参数

PVOID InterlockedCompareExchangePointer(

[in, out] PVOID volatile *Destination,

[in] PVOID Exchange,

[in] PVOID Comperand

);

参数解释

[in, out] Destination

A pointer to a PVOID value. If (*Destination) = Comperand, then the routine sets (*Destination) to Exchange.

[in] Exchange

Specifies the PVOID value to set (*Destination) to.

[in] Comperand

Specifies the PVOID value to compare with (*Destination).

返回值

dest的原始值

实际应用示例(simplewall)

current_handle = InterlockedCompareExchangePointer(
&config.hnetevent,
new_handle,
NULL
);

如果hnetevent为NULL,则把hnetevent赋值为new_handle,返回NULL

测试程序

void Lab_InterlockedCompareExchangePointer() {

    int destNum = 10;

    int exchangeNum = 20;

    int comperaNum = 30;



    int* pDest = &destNum;

    int* pExch = &exchangeNum;

    int* pComp = &comperaNum;



    int** ppDest = &pDest;



    std::cout << "pDest = " << *ppDest << std::endl;

    std::cout << "ppDest = " << ppDest << std::endl;

    std::cout << "pExch = " << pExch << std::endl;

    std::cout << "pComp = " << pComp << std::endl;



    std::cout << "执行InterlockedCompareExchangePointer" << std::endl;



    auto pOld = InterlockedCompareExchangePointer((PVOID*)ppDest, (PVOID)pExch, (PVOID)pComp);



    std::cout << "pDest = " << *ppDest << std::endl;

    std::cout << "ppDest = " << ppDest << std::endl;

    std::cout << "pExch = " << pExch << std::endl;

    std::cout << "pComp = " << pComp << std::endl;

    std::cout << std::endl;



    std::cout << "设置指针指向的值为相同,此时不会发生交换" << std::endl;

    comperaNum = 10;



    pOld = InterlockedCompareExchangePointer((PVOID*)ppDest, (PVOID)pExch, (PVOID)pComp);



    std::cout << "pDest = " << *ppDest << std::endl;

    std::cout << "ppDest = " << ppDest << std::endl;

    std::cout << "pExch = " << pExch << std::endl;

    std::cout << "pComp = " << pComp << std::endl;

    std::cout << std::endl;



    comperaNum = 30;

    std::cout << "设置指针为相同" << std::endl;

    pDest = pComp;

    pOld = InterlockedCompareExchangePointer((PVOID*)ppDest, (PVOID)pExch, (PVOID)pComp);

    std::cout << "pDest = " << *ppDest << std::endl;

    std::cout << "ppDest = " << ppDest << std::endl;

    std::cout << "pExch = " << pExch << std::endl;

    std::cout << "pComp = " << pComp << std::endl;

    std::cout << "pOld = " << pOld << std::endl;

}

输出结果

pDest = 0x5acffff914
ppDest = 0x5acffff900
pExch = 0x5acffff910
pComp = 0x5acffff90c
执行InterlockedCompareExchangePointer
pDest = 0x5acffff914
ppDest = 0x5acffff900
pExch = 0x5acffff910
pComp = 0x5acffff90c

设置指针指向的值为相同,此时不会发生交换
pDest = 0x5acffff914
ppDest = 0x5acffff900
pExch = 0x5acffff910
pComp = 0x5acffff90c

设置指针为相同
pDest = 0x5acffff910
ppDest = 0x5acffff900
pExch = 0x5acffff910
pComp = 0x5acffff90c
pOld = 0x5acffff90c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值