Benign data races?

Somebody, please elaborate on the dangers alluded to by the manual in section “Data-race freedom”

Julia is not memory safe in the presence of a data race. Be very careful about reading any data if another thread might write to it!

There are parallel algorithms that are not affected by a data race where one thread is updating a value in an array, and many threads are reading it. If some readers miss the fresh value and instead read the stale one, they will read a newer value at the next iteration. On the contrary, fewer synchronizations might be needed without affecting convergence (increasing the number of iterations) too dramatically.

That is known as a benign data race, and they have shown by the authors of C/C++/Java to lead to program mis-executions. Don’t do it. Use atomics, as the standards authors intended.

4 Likes

The manual isn’t saying that data races are logically wrong, it’s saying that Julia, as is the prevailing trend among programming languages, chooses to make data races illegal, which is done to enable more performant implementation AFAIK. I guess the optimizer has a much easier job if it can assume no races exist.

1 Like