Unsynchronized int access from threads

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jun 18 16:42:15 UTC 2020


Suppose I have an int[] which may contain some zeroes, and 2 functions,
one scans the array for zeroes but does not modify it, and the other
modifies array elements but never introduces new zeroes (though it may
write a 0 to an existing 0).  Is it thread-safe to run the two functions
in parallel without any synchronization between them?

I.e., will the first function always see zeroes where there are zeroes,
in spite of the 2nd function writing to the array simultaneously?  Are
there any hardware situations where the 1st function may read a non-zero
value if the 2nd function is simultaneously overwriting an existing zero
with another zero?  Or a situation where the 1st function may read a
zero if the 2nd function is simultaneously overwriting a non-zero value
with another non-zero value?

Or am I playing with fire here?


T

-- 
Chance favours the prepared mind. -- Louis Pasteur


More information about the Digitalmars-d mailing list