多平台原子增量
在 std::atomic
可用之前,多平台(Windows 和 Linux)原子地递增变量的方法是什么?
我目前正在使用 boost::detail::atomic_count
但它位于 boost::detail
命名空间中,我不知道它是否可以安全使用。
Until std::atomic
is available, what is the multiplatform (windows & linux) way of atomically increment a variable ?
I am currently use boost::detail::atomic_count
but it's in boost::detail
namespace and I don't know if it's safe to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种多平台但编译器特定的方法是使用 GCC 的
__sync_fetch_and_add
。或者通过一些条件编译自己定义这样的函数:
A multiplatform, but compiler specific way is to use GCC's
__sync_fetch_and_add
.Or define such a function yourself with a bit of conditional compilation:
使用InterlockedExchangeAdd而不是InterlockedIncrement - 它完全类似,带有__sync_fetch_and_add;
您可以找到经过验证的 msvc/gcc 调用类似 此处 (x86)
Use InterlockedExchangeAdd instead of InterlockedIncrement - it full analogue, with __sync_fetch_and_add;
You can find proven analogs of msvc/gcc calls here (x86)