On Monday, 5 August 2013 at 20:22:37 UTC, zyhong wrote:
From my understanding, any data type that is less than 32bit (bool, char, int, etc) is atomic and there is no need to use atomicLoad/Store and read/write the data to be thread safe. In std.parallelism, I saw it use atomicLoad/Store for any shared data. Could anyone clarify if it is really necessary to atomicLoad/Store to read/write data that datatype is bool, int, byte, etc?
You understanding is incorrect. First, non atomic read.write don't guarantee any ordering, which necessary when it come to sharing on a multiprocessor machine (almost all machines nowadays). Second, the fact that the CPU is capable of atomic read.write do not mean that the compiler will issue an atomic read/write. The compiler can : - load/store larger chunk (ie can load 32bits, modify 8bits of it and store back 32bits when you update an byte). - eliminate load/store (as they do not change the single threaded semantic). - reorder load/stores (same as above). - load/store byte by byte (unlikely, but legal). Additionally, guarantee are hardware dependent. So if some of these may not be useful on X86, which have a very strong memory model, not using atomic makes it more difficult to port D.