互锁读取 64 位变量
我有这个 C++ 代码(VS 2008):
LONGLONG res = InterlockedIncrement64(&m_longlong);
沿着它运行,我希望能够从同一个变量读取
LONGLONG res = InterlockedWHAT?64(&m_longlong)
由于这是一个 64 位变量,简单的读取不被认为是线程安全的,但我找不到正确的 InterlockedXXX 。
我应该如何读取这个变量?
I have this c++ code (VS 2008):
LONGLONG res = InterlockedIncrement64(&m_longlong);
running along it, I would like to be able to read from the same variable
LONGLONG res = InterlockedWHAT?64(&m_longlong)
Since this is a 64-bit variable, a simple read is not considered threadsafe, yet I cannot find the correct InterlockedXXX.
How should I read this variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
InterlockedOr64
并传递零作为第二个参数。据我所知,这对 Vista 没有要求,大概是因为它是用编译器内在函数实现的。You can use
InterlockedOr64
and pass zero as the second parameter. So far as I can tell this does not have a requirement of Vista, presumably as it is implemented with compiler intrinsics.如果您的程序仅在 64 位上运行,您只需读取该值即可。 MSDN 指出
If your programm runs only on 64-Bit you can simply read the value. MSDN states that
在 VS 2019 中,您应该使用
__iso_volatile_load64
,它是普通加载,因此它比InterlockedCompareExchange64
更高效With VS 2019 you should use
__iso_volatile_load64
, which is plain load, so it is more efficient thanInterlockedCompareExchange64