Are `SharedArray`s safe for simultaneous read/write from different processes?

In one process I’d like to do S[1,1] += 1; sleep(1); on a loop.
In the other process I’d like to do print(S[1,1]); sleep(0.5); on a loop.

Is this safe when S is a SharedArray? What if the attempted read by the second process happens “at the same time” as the attempted write by the first process? Do I need to use a lock or synchronization primitive to ensure that the processes will take turns in accessing S?

I found some discussion here on stackoverflow.

As answered by Tim Holy on SO, SharedArrays don’t do any locking or make use of atomics; it’s just a regular block of shared memory. Therefore, if you do any writes to the same index where another write or read is occuring, you can and probably will get undefined behavior and incorrect results. You’ll have to use an algorithm which splits up the work among threads so that each section of memory is only read or written by one thread, or implement some form of locking or synchronization via atomics, which will add some amount of overhead.

1 Like