Race condition

Race Condition

A race condition occurs when two or more threads/processes/systems can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data.

"when two threads access the same location in memory at the same time, and at least one of the accesses is a write." In the situation the "reader" thread may get the old value or the new value, depending on which thread "wins the race." 

Eg : Imagine that you have two threads.

Thread A 
if (mark.total != 0)                                                                                                                   
{                                                                                                                                              
    percentage = mark.total/no_of_subjects;                                                                          
}                                                                                                                                              

Thread B 
mark.total = 0;                                                                                                                        

If thread A is preempted just after if check, B will do mark.total = 0, and when thread A will gain the processor, it will do a "divide by zero". 

Concurrency (Interrupt-ability)

Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. Eg. multitasking on a single-core machine.

Two threads are making progress. This happens in Single processor systems. 

Parallelism (Independent-ability)

Parallelism is when tasks literally run at the same time, eg. on a multicore processor.
Two threads are executing simultaneously.  In Parallel Computing/Multi Core processor environment

Read more here.

Comments