Is there a difference between thread conflict and data race.
As per what I've learnt conflicting operations between occur when two threads try to access the same memory location and atleast one of them is a write operation.
Here is what Wikipedia has to say about data race/ race condition.
How are they different?
I have finally found a good answer to this question.
TL:DR :-
Conflicting operations -
Data race - unordered conflicting operations.
LONG VERSION -
I am explaining with an example how conflicting operations occur and how to identify if they are data race free.
Consider Thread 1
and Thread 2
, and shared variable done
.
AtomicBoolean done = new AtomicBoolean(false);
int x = 0;
Thread 1
x = f();
done.set(true);
Thread 2
while(!done.get())
{
/* a block */
}
y = g(x);
here done.set() - done.get()
and x=f() - y=g(x)
are in conflict. However the programming memory model defines 2 relations :- synchronizes-with
and happens-before
. Because the done
is atomic, its pair of operations synchronize with each other. Additionally, because of that we can choose which operation happens before
the other in that pair.
Now because x = f()
happens before done.set(true)
in Thread 1 and done.get()
happens before y = g(x)
in Thread 2, we can say x = f()
happens before y = g(x)
because happens before is a transitive relation.
Thus the above example is ordered and consequently data-race free.