I have a shared memory between multiple processes that interpets the memory in a certain way. Ex:
DataBlock {
int counter;
double value1;
double ... }
What I want is for the counter to be updated/incremented atomically. And for a memory release to happen on that address. If I werent using shared memory, for example, it would be something like
std::atomic<int> counter;
atomic_store(counter, newvalue, std::memory_order_release); // perform release operation on the affected memory location making the write visible to other threads
How do I achieve this for a random memory location (interpreted to be DataBlock counter above)? I can guarantee the address is aligned as required by the architecture (x86 Linux).
- Make the update atomic - how? (i.e.
atomicupdate(addr, newvalue)
) - Memory syncing for multicore - (i.e.
memorysync(addr)
) - only way I can see is using thestd::atomic_thread_fence(std::memory_order_release)
- but this will "establish memory synchronization ordering of ALL atomic and relaxed atomic stores" - thats overkill for me - I just want the counter location to be synchronized. Appreciate any thoughts.
atomic<int>
in your DataBlock? That should work as long asatomic<int>
is lockfree (the standard explicit mentions memory shared between processes as a use case for those). And no you can't just get an atomic for a random address (see #8749538) @Kerrek SB: actually that scenario is mentioned in [atomics.lockfree] in the final draft. – Brinton