Clarification of atomic memory access for different OSs
I'm currently porting a Windows C++ library to MacOS as a hobby project as a learning experience. I stumbled across some code using the Win Interlocked* functions and thus I've been trying to read up on the subject in general.
Reading related questions here in SO, I understand there are different ways to do these operations depending on the OS. Interlocked* in Windows, OSAtomic* in MacOS and I also found that compilers have builtin (intrinsic) operations for this.
After reading gcc builtin atomic memory access, I'm left wondering what开发者_高级运维 is the difference between intrinsic and the OSAtomic* or Interlocked* ones? I mean, can I not choose between OSAtomic* or gcc builtin if I'm on MacOS when I use gcc? The same if I'd be on Windows using gcc.
I also read that on Windows Interlocked* come as both inline and intrinsic versions. What to consider when choosing between intrinsic or inline?
In general, are there multiple options on OSs what to use? Or is this again "it depends"? If so, what does it depend on?
Thanks!
The real issue is this.
The Intel chipset which we all know and love does not have well-defined order for memory writes.
Intel® 64 Architecture Memory Ordering White Paper. This document provides an attempt to carefully define the memory write ordering.
[This document has been merged into Volume 3A of Intel 64 and IA-32 Architectures Software Developer’s Manual.]
Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 3A: System Programming Guide Part 1 This is the revision to that document.
http://www.intel.com/products/processor/manuals/
Since the chip itself doesn't guarantee specific memory write orders, it's left to the OS to guarantee consistency.
The "it depends" is "it depends on the API's in the OS for which you're building the software."
More Reading: http://en.wikipedia.org/wiki/Memory_ordering
http://rsim.cs.illinois.edu/Pubs/10-cacm-memory-models.pdf
精彩评论