c++ - Read a non-atomic variable, atomically? -
i have non-atomic 62-bit double incremented in 1 thread regularly. access not need atomic. however, variable read (not written) thread. if align variable on 64-bit boundary read atomic.
however, there way can ensure not read variable mid-way during increment? call cpu instruction serialises pipeline or something? memory barrier?
i thought of declaring variable atomic , using std::memory_order::memory_order_relaxed in critical thread (and stricter memory barrier in rare thread), seems expensive.
since tagged x86, x86-specific.
an increment 3 parts, read, add, write. increment not atomic, 3 steps (the add doesn't count suppose, it's not observable anyway) of long variable not cross cache line boundary (this condition weaker having aligned natural alignment, has been of p6, before quadwords had aligned).
so can't read torn value. worst overwrite variable in between moments read , new value written, you're reading it.
Comments
Post a Comment