c++ - Does the C++11 memory model prevent memory tearing and conflicts? -
reading draft of c++11 interested clause 1.7.3:
a memory location either object of scalar type or maximal sequence of adjacent bit-fields having non-zero width. ... 2 threads of execution (1.10) can update , access separate memory locations without interfering each other.
does clause protect hardware related race conditions such as:
- unaligned data access memory updated in 2 bus transactions (memory tearing)?
- where have distinct objects within system memory unit, e.g. 2 16-bit signed integers in 32-bit word, , each independent update of separate objects requires entire memory unit written (memory conflict)?
regarding second point, standard guarantees there no race there. being said, have been told guarantee not implemented in current compilers, , might impossible implement in architectures.
regarding first point, if second point guaranteed, , if program not contain race condition, natural outcome not race condition either. is, given premise standard guarantees writes different sub word locations safe, case can have race condition if multiple threads access same variable (that split across words, or more problematic, across cache lines).
again might hard or impossible implement. if unaligned datum goes across cache line, impossible guarantee correctness of code without imposing huge cost performance. should try avoid unaligned variables as possible , other reasons (including raw performance, write object touches 2 cache lines involves writing many 32 bytes memory, , if other thread touching of cache lines, involves cost of synchronization of caches...
Comments
Post a Comment