Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock Holder Preemption

Could you have the following scenario in concurrent programs?

suppose a thread acquires a lock to execute a critical section.Then before the critical section is executed the processor preempts the thread. The new thread that comes for execution needs the lock from the old thread (that was preempted). So the current thread can't proceed (hangs until it get preempted). Is there a mechanism in Operating systems to not let threads preempted until the lock is released?

like image 212
nmr Avatar asked Sep 19 '25 12:09

nmr


1 Answers

It is possible for a thread holding a mutex to be preempted while executing a critical section. If the thread that the OS switches to tries to acquire that mutex and finds that it is already locked, then that thread should be context switched out immediately. The thread scheduler should be smart enough to not switch back to that thread until it has switched back to the thread holding the mutex and the mutex is released.

like image 129
Daniel Avatar answered Sep 23 '25 12:09

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!