r/AskComputerScience • u/Mossack-Fonseca • Sep 16 '26
disabling interrupts to achieve mutual exclusion with user-level threads
Hi everyone,
Simple question related to operating systems. I've seen textbook authors discuss "disabling interrupts" as a method of achieving mutual exclusion. Sometimes this is discussed in the context of threads. I think this is a little bit confusing because there are a lot of different models and implementations of threads? I've seen some basic thread implementations where threads voluntarily yield control. I think this is referred to as cooperative multithreading? So the question is: with this kind of implementation, why would disabling interrupts help at all? The thread switch wouldn't occur as a response to an interrupt, so disabling interrupts shouldn't have any effect?
Does my question make sense?
as a secondary question: even in implementations where control can be taken away, is there any requirement for threads to even respond to interrupts? For example Tanenbaum says that one of the advantages of user-level threads is to have a customized scheduling algorithm. Does every thread scheduling implementation have to respond to interrupts?
5
u/DamienTheUnbeliever Sep 16 '26
Certainly in some older OSs I worked with, context switches happened when a timer interrupt fired and the scheduler decided that the current thread had had enough time and another thread should be scheduled. That can't happen if you've disabled interrupts. Most other context switches occur because the current thread puts itself into a waitable state and that won't happen if you're in the middle of doing whatever it is you disabled interrupts for.
1
u/ephemere_sloboda Sep 17 '26
Definitions can vary, but a common definition of "thread" is a context of execution which shares memory space with other related threads; typically opposed to a "process" where memory is disjoint.
With this definitions, it is possibly more relevant to talk about mutual exclusion in the context of threads, as processes have no common directly accessible resources to exclude one another from.
In a scenario with purely co-operative threads, without preemption, you simply achieve mutual exclusion by not yielding until the end of the critical section.
This is assuming single CPU core/thread as otherwise you can have multiple threads running in parallel and neither withholding yield nor masking interrupts will achieve mutual exclusion.
3
u/JoJoModding Sep 16 '26
This is about OSs for single-thread processors that use regular timer intervals to switch between threads. And of course, irregular interrupts can also happen and trigger some driver code.
In that context, you disable interrupts to ensure that your critical section is not, well, interrupted in-between. Since you are the OS, you can be sure then that nothing will interrupt you.
OSs that do cooperative multi-threading still need to worry about irregular intervals. And of course, cooperative multi-processing does not either provide CPU abstraction or resilience to bugs in userspace programs, which is why it is a bad idea for OS design.
1
u/PvtRoom Sep 21 '26
you have 30ms to respond to a safety critical signal. Failure to meet that deadline may result in death.
Tell me, do you allow a key press interrupt to pause your execution?