Is there a memory model specification for POSIX shared memory (across processes)?
I’m hoping that there’s something to comparable to the C++ memory model, in order to answer questions like the following:
- Is there a definition of a data race?
- Are data races undefined behavior (as in C++)?
- Is there something to constrain memory visibility order across processes? I know that a pthread_mutex can be put in shared memory and used by multiple processes, but does that induce a happens before relation?
- Do memory barriers play a roll?
Advertisement
Answer
According to POSIX specification, a shared memory object is defined as
An object that represents memory that can be mapped concurrently into the address space of more than one process.
On platforms as Linux, the memory object when mapped is equivalent to an anonymous mapping: this is a mapping of the physical RAM (or swap memory) to the virtual address space of the process. This is what is usually the memory of any thread or process.
On such plateforms as Linux you can expect that low memory level synchronization primitive provided by atomic will provide interprocess synchronization. According to your search pthread_mutex will work (probably because they are only implement in term of a compare and exchange loop and the futex system call). But std::mutex
may also be broken.
Nevertheless I do not see in the POSIX specification anything that would forbid shared memory to be backed by the file system. On such platform memory synchronization would be broken. But I suppose no such platform exists.