Operating System - Thread Cancellation

What is Thread Cancellation?

Thread cancellation is the process of terminating a thread before it has finished its normal execution.

In a multithreaded environment, sometimes it's necessary to cancel (stop) a thread—for example, when:

  • The result is no longer needed.

  • The application is shutting down.

  • A timeout or error occurs.

 Two Main Types of Thread Cancellation

1. Asynchronous Cancellation

  • The target thread is terminated immediately.

  • The canceling thread does not wait for the target thread to reach any specific point.

  • Can leave shared resources in an inconsistent state if the target is terminated in the middle of an operation.