Search code examples
multithreadingoperating-systemcancellation

In an operating system, who is responsible for thread cancellation?


In a multithreading environment, threads can be cancelled in one of two ways : through asynchronous cancellation or deferred cancellation. Since I have read in a reference textbook that in a two-level model for multithreading, a user thread library is responsible for threads scheduling, I was wondering who is responsible for thread cancellation in an operating system ?

Is it also the user thread library ?

This doesn't sound like a very bright idea to me since the library could very well do pretty much whatever it wants and this seems like a huge design flaw to me. (You could end up with unkillable threads wiping your entire disk !)

Could it be the OS itself ?

Then again, the OS might not be even be aware of threads (for example in a Many-to-One model), so it would end up not being able to recognize such threads.

Anyway, any enlightenment is appreciated !


Solution

  • The answer depends upon how threads are implemented. If, as you suggest, threads are created through a user library, then killing the process kills the threads. Depending upon the operating system, the library might be able to implement cleanup of the threads (rather than a sudden death).

    If the threads are implemented in the OS kernel, the operating system must handle the thread termination.