Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a race and a dead lock

In concurrent programming, what are the differences and the common points (if any) between a race and a dead lock ? An detailed answer would be appreciated ;).

like image 447
Jean-Bernard Jansen Avatar asked Nov 29 '10 15:11

Jean-Bernard Jansen


1 Answers

Have a look at Description of race conditions and deadlocks

Race Conditions

A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.

Deadlocks

A deadlock occurs when two threads each lock a different variable at the same time and then try to lock the variable that the other thread already locked. As a result, each thread stops executing and waits for the other thread to release the variable. Because each thread is holding the variable that the other thread wants, nothing occurs, and the threads remain deadlocked.

like image 184
Adriaan Stander Avatar answered Sep 27 '22 22:09

Adriaan Stander