Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does this happen? Thread suspended while in critical section

I'm just wondering, if a thread is in a critical section, can it be preempted?

  • Thread A: Enter CR
  • Thread A: Get suspended
  • Thread B: Wants to enter CR but can't, because Thread A has the lock

If Thread A preempted, and so the mutex lock is stuck with Thread A, what can be done about this?

like image 405
bobobobo Avatar asked Oct 16 '25 14:10

bobobobo


1 Answers

Suppose thread A is preempted by higher priority thread C. Now suppose thread B is in fact higher priority than C. If B becomes runnable, you have a classic case of priority inversion; Thread B (high priority) is stuck waiting for a resource held by Thread A (low priority). One cure for this is called priority inheritance.

With priority inheritance, when B blocks for the resource held by A (the critical section), thread A temporarily 'inherits' the priority of thread B. This allows A to preempt that bothersome middle-priority thread C, and when A is done with the resource, A goes back to its original priority. This gets A out of B's way, so to speak, eliminating the dead-lock.

like image 171
JustJeff Avatar answered Oct 19 '25 14:10

JustJeff