I’m two months late, but I’m having the exact same problem right now and I think I’ve found some sort of an answer. The short version is that it should work, but I’m not sure if I’d depend on it.
Here’s what I found:
-
The C++11 standard defines a new memory model, but it has no notion of OS-level “process”, so anything multiprocessing-related is non-standard.
-
However, section 29.4 “Lock-free property” of the standard (or at least the draft I have, N3337) ends with this note:
[ Note: Operations that are lock-free should also be address-free. That is, atomic operations on the same
memory location via two different addresses will communicate atomically. The implementation should not
depend on any per-process state. This restriction enables communication by memory that is mapped into a
process more than once and by memory that is shared between two processes. — end note ]This sounds very promising. 🙂
-
That note appears to come from N2427, which is even more explicit:
To facilitate inter-process communication via shared memory, it is our intent that lock-free operations also be address-free. That is, atomic operations on the same memory location via two different addresses will communicate atomically. The implementation shall not depend on any per-process state. While such a definition is beyond the scope of the standard, a clear statement of our intent will enable a portable expression of class of a programs already extant.
So it appears that yes, all lock-free operations are supposed to work in this exact scenario.
-
Now, operations on
std::atomic<type>
are atomic but they may or may not be lock-free for particulartype
, depending on capabilities of the platform. And We can check any variablex
by callingx.is_lock_free()
. -
So why did I write that I would not depend on this? I can’t find any kind of documentation for gcc, llvm or anyone else that’s explicit about this.