Quantcast
Channel: What is a race condition? - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by bharanitharan for What is a race condition?

$
0
0

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


Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>