
- Operating System Tutorial
- OS - Home
- OS - Overview
- OS - Components
- OS - Types
- OS - Services
- OS - Properties
- OS - Processes
- OS - Process Scheduling
- OS - Scheduling algorithms
- OS - Multi-threading
- OS - Memory Management
- OS - Virtual Memory
- OS - I/O Hardware
- OS - I/O Software
- OS - File System
- OS - Security
- OS - Linux
- OS - Exams Questions with Answers
- OS - Exams Questions with Answers
- Operating System Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
What is Thread cancellation?
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 −
Example
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.
Mode | State | Type |
---|---|---|
Off | Disabled | - |
Deferred | Enabled | Deferred |
Asynchronous | Enabled | Asynchronous |
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(); }
- Related Articles
- Explain the process of cancellation of numbers.
- What is the concept of thread?
- What is the Thread class in Java?
- What is a daemon thread in Java?
- What is Thread Testing in Software Testing?
- What are thread libraries?
- When a thread is created and started, what is its initial state?
- What method is used to create a daemon thread in Java?
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- Is Swing thread-safe in Java?
- What are the ways in which a thread is created in Java?
- How to check whether a thread is a background thread or not in C#
- Main thread vs child thread in C#
- User Thread vs Daemon Thread in Java?
- What are the differences between yarn and thread?
