multithreading - C++: Fetch_add on memory mapped file -
multithreading - C++: Fetch_add on memory mapped file -
i openend file using boost mapped-file library. possible utilize "fetch_add" (value read @ position added , written same position atomically) on mapped file?
if multiple threads write in parallel there problems without atomicity involved
the file in binary format , contains ints or doubles (depends on specific file).
i tried locks/mutexes slow programme downwards when using multiple threads. time spent in locked regions big compared rest of algorithm , threads block each other.
are there improve ways multiple threads can write mapped file high performance?
thanks. laz
are there multiple processes mapping file, or multiple threads?
if multiple processes accessing memory mapped file concurrently, you'll have own (inter-process) synchronization.
if it's multiple threads, can atomically update memory same way you'd other word of memory, caveat can't utilize std::atomic
(because bytes correspond straight section in file, , not std::atomic
structures). so, must resort using specific platform's back upwards atomically modifying memory, namely lock xadd
on x86 via, e.g., interlockedincrement
on win32 (or __sync_fetch_and_add
g++). careful ensure memory ordering semantics (and homecoming value!) expect.
wrapping platform-specific functions in platform-independent way (if need that) can bit of hassle, though, , in case i'd suggest keeping concurrently-accessed info in separate std::atomic
variables, updating corresponding file bytes 1 time @ end.
note of orthogonal memory mapping -- os backs memory-mapped file pages swaps in , out on demand, , memory management unit manages pages same 1 handles arbitrary other (non-mapped) pages, hence pages can modified multiple threads without having worry other usual (application-level) info races.
c++ multithreading atomic memory-mapped-files
Comments
Post a Comment