Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hatomic.h windows下原子操作错误 #581

Open
Cracal opened this issue Jul 7, 2024 · 1 comment
Open

hatomic.h windows下原子操作错误 #581

Cracal opened this issue Jul 7, 2024 · 1 comment

Comments

@Cracal
Copy link

Cracal commented Jul 7, 2024

不同长度的整型使用这些原子操作接口时得到的结果不正确。
比如:atomic_flag_test_and_set函数(内部代码:InterlockedCompareExchange((LONG*)&p->_Value, 1, 0))调用后并不能让atomic_flag置1。

还有这些原子操作接口在不同平台下的返回值不同。比如我用来实现引用计数的代码:

if (ATOMIC_DEC(&ref->refcnt) == 1)
{
    release_cb(ref);
}

在linux下,ATOMIC_DEC返回的减少之前的值,而在windows下返回的是减少之后的值,这导致这段代码在Windows下不正确。

@ithewei
Copy link
Owner

ithewei commented Jul 8, 2024

libhv里确实没有用到ATOMIC_INC、ATOMIC_DEC的返回值,这里windows下可以考虑使用InterlockedAdd,而不是InterlockedIncrement/InterlockedDecrement,应该就可以了吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants