Are “data races” and “race condition” actually the same thing in context of concurrent programming

后端 未结 4 2035
一整个雨季
一整个雨季 2020-11-30 17:03

I often find these terms being used in context of concurrent programming . Are they the same thing or different ?

4条回答
  •  余生分开走
    2020-11-30 17:44

    According to Wikipedia, the term "race condition" has been in use since the days of the first electronic logic gates. In the context of Java, a race condition can pertain to any resource, such as a file, network connection, a thread from a thread pool, etc.

    The term "data race" is best reserved for its specific meaning defined by the JLS.

    The most interesting case is a race condition that is very similar to a data race, but still isn't one, like in this simple example:

    class Race {
      static volatile int i;
      static int uniqueInt() { return i++; }
    }
    

    Since i is volatile, there is no data race; however, from the program correctness standpoint there is a race condition due to the non-atomicity of the two operations: read i, write i+1. Multiple threads may receive the same value from uniqueInt.

提交回复
热议问题