Are 64 bit operations atomic for a 32 bit app on 64 bit Windows

こ雲淡風輕ζ 提交于 2019-12-23 03:14:51

问题


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

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

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?


回答1:


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.

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.

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.

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.

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.



来源:https://stackoverflow.com/questions/27533611/are-64-bit-operations-atomic-for-a-32-bit-app-on-64-bit-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!