Race Condition

Running multiple threads are fine, but the issue arises when multiple access to the same resource e.g. memory component a variable, array, or a database system web service. The key issue is multiple threats to alter the same resource. If there is no alteration in the target resource, then multiple threads are good.

 Example situation

Imagine two threads; Thread A and Thread B, are accessing a variable called “Counter”, and reaching the variable A. So the scenario is amount of counter will be increased. When multiple threads are implemented, traditional line by line process is invalid, however operating system handles two different threads individually each has its own boundary. The pseudo code and the steps as follow accordingly;

1-Initial value of Counter is 0

2-Thread A reads the value of Counter as 0,

3-Thread B reads the value of Counter as 0,

4-Thread A increases the value of Counter to 2,

5-Thread B increases the value of Counter to 3,

 

Normally the last value is supposed to be 5, but since Thread B has read the value as 0, it will opt out the previous given value 2 and write the the counter as 3. Without proper use of synchronization keyword, this issue will go on.

Leave a Reply

Your email address will not be published. Required fields are marked *