How bad are data races?
-
Can a data race such as when 1 thread reads an int from memory and another thread writes to the same address at the same time cause any real harm, except the obvious data inconsistency?
-
@Grand_Titan You basically get to undefined behavior, so theoretically almost anything could happen including a hard crash or silent data corruption.
In practice, you are likely to get some sort of scrambled nonsense value for the integer which might be fine in some scenarios. If the int is the number of items in an array, you will likely wind up reading/writing past the end of the array and having a buffer overflow issue. If you just display the value of the int on screen, your app may blip and say the temperature outside is negative 3 billion degrees or something. But it's almost impossible to be certain that any given value is never going to be used in a way that could be a problem, so you generally need to fix these sorts of bugs.
-
Depending on bunch of factors, like the cpu architecture and data alignment, a read/write to an int can be atomic, so sometimes it's ok. But sometimes it's not and that's when fun begins. It's undefined behavior land. Problems range from harmless small inconsistencies to major security vulnerabilities.
As a simple example imagine writing a size of a container during shrinking operation and reading number of elements at the same time to read from the last one. Again depending on bunch of factors this can lead to reading/writing out of bounds, which, if it happens not to crash your app, is a serious vulnerability. Other example are data races in locking mechanisms that can result in various desynchronization scenarios.
It all varies, depending on what your int is doing and how far reaching influence it has on the entire system.
So how bad are data races? As a rule of thumb - Very bad. Fix it.
-
Thank you for clearing this up for me.
-
-
The x86 architecture has some really weird rules for data races. There are even combinations that seem to be totally illogical at first (check out slide 29 here: https://www.cse.unsw.edu.au/~cs9242/23/lectures/07b-smp.pdf). During optimization compilers (and CPU cores themselves) can do instruction reordering because there are no dependencies between variables inside a single thread.
Any data race can be really hard to debug (especially because it is usually not deterministic!). So, I agree with @Chris-Kawa: "Very bad. Fix it."