As we can see from the results, the value of unsafeResultArray has been corrupted by a race condition. It looks as if some of the values are missing. However, looking at the worker it appears as if we are adding the result as a single operation.
This isn't precisely true. The += operator is actually three separate operations, a read, an addition, and a write operation. If you imagine that multiple workers reach this segment at the same time (we have 256 operating at once), then you can imagine how a race condition can occur.
Atomics prevent these errors from happening. Atomic.add, for example, operates as if +, = were a single operation. When a worker uses Atomics.add or any other method in the API, they can be sure that the ...