What is Thread cancellation?

Operating SystemWindowsMCA

Terminating a thread before it has completed is called Thread cancellation. For an example, if multiple threads are concurrently searching through a database and one thread returns the result, the remaining threads might be canceled. Another situation might be occurred when a user presses a button on a web browser that stops a web page from loading any further. Often, using several threads a web page loads — each image is loaded in a separate thread. When the stop button is pressed by a user on the browser, all threads loading the page are canceled. A thread which is to be cancelled is often referred to as the target thread. Cancellation of a target thread may occur in two different cases −

  • Asynchronous cancellation − One thread terminates immediately the target thread.

  • Deferred cancellation − The target thread checks periodically whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion.

The difficulty with cancellation occurs in situations where resources have been allocated to a cancelled thread or where a thread is canceled while in the midst of updating data it is sharing with other threads. This becomes especially troublesome with asynchronous cancellation. Many times, the operating system will reclaim system resources from a cancelled thread but will not reclaim all resources. So, canceling a thread asynchronously may not free a necessary system-wide resource. In contrast, with deferred cancellation, one thread indicates that a target thread is to be canceled, but cancellation occurs only after the target thread has checked a flag to determine whether or not it should be canceled. This check can be performed by the thread at a point at which it can be canceled safely. In Pthreads, thread cancellation is initiated using the pthread cancel() function. The identifier of the target thread is passed as a parameter to the function. The following code illustrates creating—and then canceling— a thread −


pthread t tid;
/* create the thread */
pthread create(&tid, 0, worker, NULL);
/* cancel the thread */
pthread cancel(tid);

Invoking pthread cancel() indicates only a request to cancel the target thread, however; actual cancellation depends on how the target thread is set up to handle the request. Pthreads supports three cancellation modes. Each mode is defined as a state and a type, as illustrated in the table below. Its cancellation state and type may be set by a thread using an API.


As the table illustrates, Pthreads allows threads to disable or enable cancellation. Obviously, a thread cannot be canceled if cancellation is disabled. However, cancellation requests remain pending, so the thread can later enable cancellation and respond to the request. The default cancellation type is deferred cancellation. Here, cancellation occurs only when a thread reaches a cancellation point. One technique for establishing a cancellation point is to invoke the pthread testcancel() function. A function known as a cleanup handler is invoked, if a cancellation request is found to be pending. Any resources a thread may have acquired is allowed by this function to be released before the thread is terminated. The following code illustrates how a thread may respond to a cancellation request using deferred cancellation −

while (1){
   /* do some work for a while */
   /* ... */
   /* check if there is a cancellation request */
   pthread testcancel();
Updated on 31-Jan-2020 11:07:24