64 位 Windows 上的 32 位应用程序的 64 位操作是原子操作吗 [英] Are 64 bit operations atomic for a 32 bit app on 64 bit Windows

查看:25
本文介绍了64 位 Windows 上的 32 位应用程序的 64 位操作是原子操作吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个文档说运行 64 位 Windows 给你 64 位原子性:http://msdn.microsoft.com/en-us/library/windows/desktop/ms684122%28v=vs.85%29.aspx

So this document says that running 64 bit Windows gives you 64 bit atomicity: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684122%28v=vs.85%29.aspx

这篇文章表明您必须运行 64 位应用程序才能获得 64 位原子性:原子性32/64 位

This post indicates that you have to run a 64 bit app to gain 64 bit atomicity: atomicity in 32/64 bit

我正在开发一个 Win32 控制台应用程序.所以,如果我理解正确,我必须使用 32 位类型来获得原子性,对吗?我不能假设 64 位类型具有原子写入/读取?

I'm developing a Win32 console app. So, if I understand correctly, I have to use 32 bit types to get atomicity, right? I cannot assume a 64 bit type has atomic writes/reads?

推荐答案

在 64 位应用程序中,64 位读/写操作可以是自动的,因为编译器可以利用具有原子 64 位的 x64 扩展指令集-位读/写操作.

In a 64-bit app, 64-bit read/write operations can be automatic because the compiler can take advantage of the x64 extended instruction set which has atomic 64-bit read/write operations.

在 64 位操作系统上的 32 位代码中硬件,另一方面,如果应用程序需要读/写 64 位数据,则​​没有可用的 64 位读/写指令,因此编译器必须(至少)生成两个读/写操作.由于操作系统可以在两次读取/写入之间抢占进程,因此您应该使用 Interlockedxxx API.

In 32-bit code on a 64-bit OS & hardware, on the other hand, if the app needs to read/write 64-bit data, there are no 64-bit read/write instructions available so the compiler has to generate (at least) two read/write operations. Because the OS could pre-empt the process in between the two reads/writes, you should employ the Interlockedxxx API's.

注意:您可以构建64位Win32如果需要,控制台应用程序.在这种情况下,编译器可以生成使用 64 位读/写操作的代码.

Note: You can build 64-bit Win32 console apps if you want. In this case, the compiler can generate code that uses the 64-bit read/write op's.

当然,由于您的代码可能要读取/写入大于 64 位的数据类型(例如 SSE2/3、AVX 等),因此需要多个读取/写入操作,您应该 使用适当的内在函数 以确保所需的操作是原子的.

Of course, since your code might want to read/write data types that are larger than 64-bit (e.g. SSE2/3, AVX, etc), thus requiring multiple read/write op's, you should employ the appropriate intrinsics to ensure that the required operations are made atomic.

与其假设您可以依靠编译器为您的特定场景做正确的事情,我建议您使用必要的 API 和内在函数来明确声明您的哪些操作必须是原子的.这样你就不会关心你的代码是否是为 x64、x64、ARM、MIPS 等编译的.

Rather than assume that you can rely on the compiler to do the right thing for your specific scenario, I'd recommend employing the necessary API's and intrinsics to explicitly declare which of your operations MUST be atomic. This way you won't care if your code is compiled for x64, x64, ARM, MIPS, etc.

这篇关于64 位 Windows 上的 32 位应用程序的 64 位操作是原子操作吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆