3 ms·
You can do it with shared memory. One writer writes and multiple readers can observe. I did this for both low latency and throughput reasons. In general you ha
by rdtsc 11y ago
You can do it with shared memory. One writer writes and multiple readers can observe. I did this for both low latency and throughput reasons.
In general you have to be very careful how you handle it and consider various consistency and failures scenarios.
The main part of memory layout looks something like this:
[write_counter][......buffer.....]
This is owned and updated by the writer. Readers have a read_counter that they maintain in their own context (not shared).
You'd probably have to declare this using the 'volatile' keyword. Otherwise compilers will optimize away access to part of this (seemingly) unused variables.
Then it works like this:
The writer_counter value is always counting up when writer writes. Data gets written to buffer. Then "write_counter" is incremented. Both reader and writer index into buffer by using {write|reader}_counter%buffer_size.
Also, note these counters also function as total counts of items written and so each reader cand determine how far ahead the writer is.
Another note: depending on the sizes of your counter it will not necessarily be updating atomically. Compiler could separate the update as multiple instructions and say, increment the lower part of the value, then the upper part. Writer could get pre-empted between those two instructions, so you could get this strange torn value. In this case because of the % you'd still fall into the range of the buffer. But, you might be reading data you didn't expect. Whether that works for your use case or not you'll have to see.
EDIT: Don't also forget about the slow and stupid multicast mechanism -- writing to a file. Some file operations can be atomics (renaming a file). And some operating systems let you watch the files for changes.