GCC 新原子整数运算的替代方案
GCC 最近支持原子操作(如所述此处)非常棒,可以满足我们 90% 的需求。不幸的是,我们的一些产品仍然需要在 Windows 上运行,因此我们也需要 Windows 的原子整数操作。
过去,我们为所有平台提供了自定义汇编语言实现,但我想将所有 *nix 平台移至 GCC 支持的操作,并且我想到也许还有一种更标准的方法来做到这一点现在也在 Windows 上...
是否有官方认可的方法可以在 Windows 上执行此操作(除了自己用汇编语言实现它们之外)?
GCC's recent support for atomic operations (as described here) is great, and is 90% of what we need. Unfortunately, some of our products still need to run on Windows and so we need atomic integer operations for Windows as well.
In the past, we had custom assembly language implementations for all our platforms, but I'd like move all the *nix platforms over to the GCC supported operations and the thought crossed my mind that perhaps there is also a more standard way to do this on Windows now as well...
Is there an officially sanctioned way of doing this on Windows (other than implementing them yourself in assembly language)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要互锁函数。
You want the Interlocked functions.
这取决于您需要哪一个——我认为 Windows 上没有(预构建的)实现它们中的每一个(除了可能在 gcc 内),但其中一些已经存在很长一段时间了。 Windows有InterlockedIncrement、InterlockedDecrement、InterlockedAdd、InterlockedOr、InterlockedXor等。
It depends on which of those you need -- I don't think there's a (pre-built) implementation of every one of them on Windows (except possibly within gcc) but some of them have been around for quite a while. Windows has InterlockedIncrement, InterlockedDecrement, InterlockedAdd, InterlockedOr, InterlockedXor, and so on.
GLib 提供了一些原子操作,并且它们是已知的在 Windows 上运行。您可以检查来源以获取想法或直接选择代码你需要。
GLib provides some atomic operations, and they are known to run on windows. You can inspect the sources for ideas or directly pick the code you need.