Can we have race conditions in a single-thread program?

前端 未结 3 813
悲哀的现实
悲哀的现实 2020-12-02 17:18

You can find on here a very good explanation about what is a race condition.

I have seen recently many people making confusing statements about race conditions and t

3条回答
  •  旧时难觅i
    2020-12-02 18:10

    Yes.

    A "race condition" is a situation when the result of a program can change depending on the order operations are run (threads, async tasks, individual instructions, etc).

    For example, in Javascript:

    setTimeout(() => console.log("Hello"), 10);
    setTimeout(() => setTimeout(() => console.log("World"), 4), 4);
    
    // VM812:1 Hello
    // VM812:2 World
    
    setTimeout(() => console.log("Hello"), 10);
    setTimeout(() => setTimeout(() => console.log("World"), 4), 4);
    
    // VM815:2 World
    // VM815:1 Hello
    

    So clearly this code depends on how the JS event loop works, how tasks are ordered/chosen, what other events occurred during execution, and even how your operating system chose to schedule the JS runtime process.

    This is contrived, but a real program could have a situation where "Hello" needs to be run before "World", which could result in some nasty non-deterministic bugs. How people could consider this not a "real" race condition, I'm not sure.

    Data Races

    It is not possible to have data races in single threaded code.

    A "data race" is multiple threads accessing a shared resource at the same time in an inconstant way, or specifically for memory: multiple threads accessing the same memory, where one (or more) is writing. Of course, with a single thread this is not possible.

    This seems to be what @jillro's answer is talking about.


    Note: the exact definitions of "race condition" and "data race" are not agreed upon. But if it looks like a race condition, acts like a race condition, and causes nasty non-deterministic bugs like a race condition, then I think it should be called a race condition.

提交回复
热议问题