What is a race condition?

后端 未结 18 2566
谎友^
谎友^ 2020-11-21 04:52

When writing multithreaded applications, one of the most common problems experienced is race conditions.

My questions to the community are:

What is the rac

18条回答
  •  一生所求
    2020-11-21 05:16

    Consider an operation which has to display the count as soon as the count gets incremented. ie., as soon as CounterThread increments the value DisplayThread needs to display the recently updated value.

    int i = 0;
    

    Output

    CounterThread -> i = 1  
    DisplayThread -> i = 1  
    CounterThread -> i = 2  
    CounterThread -> i = 3  
    CounterThread -> i = 4  
    DisplayThread -> i = 4
    

    Here CounterThread gets the lock frequently and updates the value before DisplayThread displays it. Here exists a Race condition. Race Condition can be solved by using Synchronzation

提交回复
热议问题