c# - are reads and writes to a variable of type double guaranteed to be atomic on a 64 bit intel processor? -
i have 64 bit machine running 64 bit processor application 32 bit. reading or writing double guaranteed atomic? talking assignment , reading only. how 32 bit process affect process? can reader ever read partial value double when other thread writing in given specs?
no. double can straddle l1 cache line boundary, requiring multiple bus cycles glue 2 pieces together. real problem in c#, 32-bit clr provides alignment guarantee of 4.
these misaligned accesses not not atomic, expensive due shuffling processor needs do. code uses double can have 3 distinct timings, fast if double happens aligned 8 accident, twice slow when misaligned 4 still inside l1 cache line, on 3 times slower when misaligned across cache line. such program can randomly bump 1 of these modes when garbage collector compacts heap , moves double. very wary of this, synthetic test program can miss failure mode.
the perf problem core reason double[] allocated in large object heap sooner normal. loh provides alignment 8 guarantee. normal rule objects >= 85,000 bytes, double[] in 32-bit mode 8,000 bytes (1000 elements in array).
you must use interlocked.exchange() if need atomicity guarantee. , ought post disclaimer atomicity weak guarantee , no substitute proper locking when requried.
Comments
Post a Comment