C ++ / Linux: using the C ++ 11 atom to avoid partial reading in the bidirectional mmap scope

I have a program that has two threads. One stream (Writer Thread) writes to the file, and the other consuming (read stream) - data from the first. The program displays the same file area twice: one with read and write permissions for Writer Thread, the other with read permissions for Thread Reader. (The two displayed regions have a different pointer / virtual address from mmap, as expected). I am trying to use a C ++ 11 atom to control memory order.

Here is what I mean:

Writer Thread:

  • Create data content (fixed size) in a memory-mapped area with write permission.
  • Update atomic variable with memory deallocation order.

Reader Subject:

  • Continuous polling by atomic variable with memory order until new messages appear.
  • If the message is outstanding, read data from a read-only memory area.

Questions

  • Although the read-only mmap area and the mmap write area all refer to the same file area, they have different virtual memory addresses. Can an atomic variable protect partial reading here? (i.e., if the reader thread sees that the atomic variable is updated using receive semantics, will the read-only area only have a partial message or is the message not yet visible at all?) (It seems to me that if two virtual memories are mapped onto one and same physical memory page, it should work.)
  • , Reader Thread mmap ? ?

, , , . , , / Linux , . !

+4
3

. , , . .

, .

read() , . memcpy(), . , .

0

. , . stadard: 29.3.2 2 A, M, B, M , A   , , , , , .

0

, . . , .

.

, , , , . .

, read() write() , , PIPE_BUF. (4K Linux, ). . , , memcpy 4K, , .

Volatile - , .

On the side of the note, with exactly the same design on AIX, we saw a huge performance degradation compared to a small design change when the author uses a write()memory-mapped file to update immediately. Not sure if this is AIX quirk - but if performance is important, you might want to benchmark it.

-4
source

Source: https://habr.com/ru/post/1615044/


All Articles