Hacker News new | ask | show | jobs
by Rusky 2245 days ago
Data races are a kind of race condition, no?
2 comments

I can convince myself that data races need not be a race condition. Consider this simple program:

    var i int
    doneCh := make(chan struct{})
    go func() { i = 1; doneCh <- struct{}{} }() // a
    go func() { i = 1; doneCh <- struct{}{} }() // b
    <-doneCh
    <-doneCh
At the end of the program, i is always equal to 1 no matter which order a or b wrote to i. But it's a race because you are assigning to a shared variable without synchronization. A small modification to the program creates a race condition:

    var i int
    doneCh := make(chan struct{})
    go func() { i = 1; doneCh <- struct{}{} }() // a
    go func() { i = 2; doneCh <- struct{}{} }() // b
    <-doneCh
    <-doneCh
Is i 1 or 2? It depends.

It is correct for the race checker to complain about the first program, because after a bit of hacking the first program can very easily change into the second program.

(And I tried it, and it does complain.)

I wasn't sure. After a bit of research, this seems to be a debate [1]. Using the common definitions, it's possible to have a data race that doesn't cause a race condition. [2] It's also possible to have a race condition without a data race.

[1] https://en.wikipedia.org/wiki/Race_condition#Data_race

[2] https://blog.regehr.org/archives/490

I would argue that "a data race that doesn't cause a race condition" is still, itself, a tiny race condition- just a contained one.

But you're right, this is just choice of terminology. :)