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

查看:196
本文介绍了对于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.

在32位代码上, 64位操作系统硬件方面,如果应用程序需要读取/写入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天全站免登陆