InterlockedDecrement用法注意的事项 .

探讨了多线程环境下使用InterlockedDecrement进行引用计数时可能引发的重复删除问题,并提供了一种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在多线程编程中,我们经常用到InterlockedDecrement , 此处有个细节问题,简单聊一下.

下面的程序用mReferenceCount来控制是否要删除自己.

    void AddRef()

{
InterlockedIncrement(&mReferenceCount);
}


void Release()
{
InterlockedDecrement(&mReferenceCount);
if (0 == mReferenceCount)
{
delete this;
}

}

猛的一看,好像没什么问题, 大家看Release这个函数, InterlockedDecrement是个原子操作,假设当两个线程到到达这个函数时,

        mReferenceCount为2, 只有一个线程要执行InterlockedDecrement操作, 那么mReferenceCount减一为 1, 这个线程继续往下

执行,当正在比较操作时, 第二个线程也执行了InterlockedDecrement操作,那么mReferenceCount再减一为0. 好了问题出现了.

       第一个线程在比较操作时, 发现mRerenceCount为0,就去执行delete操作. 当第二个线程到达比较操作时, 发现mReferenceCount

也为0, 也去执行delete操作,连续两次delete, 程序就崩溃了.

   这个问题很难发现,所以写了博克, 给诸位同行提个醒.

下面是解决方案:

void Release()
{
long refCount = InterlockedDecrement(&mReferenceCount);
if (0 == refCount)
{
delete this;
}
}

我们加入了refCount局部变量,也就是说每个线程在Release函数中各自维护一个计数结果, 这样就避免了重复删除.

C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9878): error C2065: “InterlockedIncrement64”: 未声明的标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9910): error C2065: “InterlockedDecrement64”: 未声明的标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9946): error C3861: “InterlockedExchange64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(10000): error C3861: “InterlockedExchangeAdd64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(10010): error C3861: “InterlockedExchangeAdd64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(10057): error C3861: “InterlockedAnd64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(10067): error C3861: “InterlockedOr64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(10077): error C3861: “InterlockedXor64”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt\wrl\implements.h(1570): error C3861: “InterlockedCompareExchangeNoFence”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt\wrl\implements.h(1588): error C3861: “InterlockedCompareExchangeRelease”: 找不到标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt\wrl\implements.h(1874): error C2065: “_ARM64_BARRIER_ISH”: 未声明的标识符 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt\wrl\implements.h(1874): error C3861: “__dmb”: 找不到标识符 src\Candidate\EnumTfCandidates.cpp(78): error C2665: “InterlockedIncrement”: 没有重载函数可以转换所有参数类型 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9874): note: 可能是“unsigned __int64 InterlockedIncrement(volatile unsigned __int64 *)” src\Candidate\EnumTfCandidates.cpp(78): note: “unsigned __int64 InterlockedIncrement(volatile unsigned __int64 *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned __int64 *” src\Candidate\EnumTfCandidates.cpp(78): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9862): note: 或 “unsigned long InterlockedIncrement(volatile unsigned long *)” src\Candidate\EnumTfCandidates.cpp(78): note: “unsigned long InterlockedIncrement(volatile unsigned long *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned long *” src\Candidate\EnumTfCandidates.cpp(78): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9853): note: 或 “unsigned int InterlockedIncrement(volatile unsigned int *)” src\Candidate\EnumTfCandidates.cpp(78): note: “unsigned int InterlockedIncrement(volatile unsigned int *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned int *” src\Candidate\EnumTfCandidates.cpp(78): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 src\Candidate\EnumTfCandidates.cpp(78): note: 尝试匹配参数列表“(LONG *)”时 src\Candidate\EnumTfCandidates.cpp(83): error C2665: “InterlockedDecrement”: 没有重载函数可以转换所有参数类型 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9906): note: 可能是“unsigned __int64 InterlockedDecrement(volatile unsigned __int64 *)” src\Candidate\EnumTfCandidates.cpp(83): note: “unsigned __int64 InterlockedDecrement(volatile unsigned __int64 *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned __int64 *” src\Candidate\EnumTfCandidates.cpp(83): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9894): note: 或 “unsigned long InterlockedDecrement(volatile unsigned long *)” src\Candidate\EnumTfCandidates.cpp(83): note: “unsigned long InterlockedDecrement(volatile unsigned long *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned long *” src\Candidate\EnumTfCandidates.cpp(83): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um\winbase.h(9885): note: 或 “unsigned int InterlockedDecrement(volatile unsigned int *)” src\Candidate\EnumTfCandidates.cpp(83): note: “unsigned int InterlockedDecrement(volatile unsigned int *)”: 无法将参数 1 从“LONG *”转换为“volatile unsigned int *” src\Candidate\EnumTfCandidates.cpp(83): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 src\Candidate\EnumTfCandidates.cpp(83): note: 尝试匹配参数列表“(LONG *)”时 > in src\Candidate\EnumTfCandidates.cpp PS D:\Downloads\MetasequoiaImeTsf-master\MetasequoiaImeTsf - 副本>
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值