On Linux I use shmgetand shmatto adjust the shared memory segment, which will be recorded one process and one or more processes will be read. The data that is shared is a few megabytes, and when the update is completely rewritten; It is never partially updated.
shmget
shmat
I split the shared memory segment as follows:
------------------------- | t 0 | actual data | t 1 | -------------------------
where t 0 and t 1 are copies of the time when the author started the update (with sufficient accuracy so that successive updates are guaranteed to be different from each other). First, the record is written to t 1 , then copied to the data, then written to t 0 . On the other hand, the reader reads t 0 , then data, then t 1 . If the reader receives the same value for t 0 and t 1 , then he considers the data consistent and valid, if not, he tries again.
This procedure ensures that if the reader considers the data valid, is it really?
(OOE)? , , memcpy, OOE ? (, memcpy . ?)
memcpy
: " / " .
..
., , .
, , t 0 t 1.
, . . x86/x64, - *.
* , /JIT , . volatile ( Java #, ISO C/++. , . , V++ 2005 , . . " Microsoft" . x86/x64. , , to t 0 t 1 .)
volatile
, - MFENCE, lock or [TopOfStack],0 , .
MFENCE
lock or [TopOfStack],0
, . , , . , , . OoO, . . : . , .
, , , , , , , , . , , , , . ntpd . Linux, clock_gettime (CLOCK_MONOTONIC,...). , . , . , , , , , , . , , , .
Linux , seqlocks, - . " GPL", Google ; , , .