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?
Of course it can be preempted. Otherwise how the other threads could try to enter that critical section, if the only thread that is allowed to run within the process is the thread that owns the critical section?
Thread B in your example will wait until thread A is rescheduled and is finished with the crtical section. No surprise here. And if thread A, while in a critical section, also waits for a mutex owned by thead B, then it’s a deadlock which you must resolve by revising your logic.