Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Thread cancellation?
Terminating a thread before it has completed is called thread cancellation. For example, if multiple threads are concurrently searching through a database and one thread returns the result, the remaining threads might be canceled. Another situation occurs when a user presses a stop button on a web browser to halt page loading. Often, a web page loads using several threads ? each image is loaded in a separate thread. When the stop button is pressed, all threads loading the page are canceled.
A thread that is to be canceled is often referred to as the target thread. Cancellation of a target thread may occur in two different ways:
Types of Thread Cancellation
Asynchronous cancellation ? One thread immediately terminates the target thread without warning.
Deferred cancellation ? The target thread checks periodically whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion.
Challenges with Thread Cancellation
The difficulty with cancellation occurs when resources have been allocated to a canceled thread or when a thread is canceled while updating shared data. This becomes especially troublesome with asynchronous cancellation. The operating system will reclaim some system resources from a canceled thread but may not reclaim all resources, potentially leaving necessary system-wide resources unavailable.
With deferred cancellation, one thread indicates that a target thread should be canceled, but cancellation occurs only after the target thread checks a flag at a safe cancellation point. This allows the thread to clean up resources and terminate safely.
Pthreads Cancellation
In Pthreads, thread cancellation is initiated using the pthread_cancel() function. The identifier of the target thread is passed as a parameter. 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. Actual cancellation depends on how the target thread is configured to handle the request.
Pthreads Cancellation Modes
Pthreads supports three cancellation modes, each defined by a state and type:
| Mode | State | Type |
|---|---|---|
| Off | Disabled | ? |
| Deferred | Enabled | Deferred |
| Asynchronous | Enabled | Asynchronous |
Threads can disable or enable cancellation. A thread cannot be canceled if cancellation is disabled, but cancellation requests remain pending. The thread can later enable cancellation and respond to the request. The default cancellation type is deferred cancellation.
Deferred Cancellation Example
With deferred cancellation, cancellation occurs only when a thread reaches a cancellation point. One technique for establishing a cancellation point is to invoke the pthread_testcancel() function. If a cancellation request is pending, a cleanup handler is invoked to release any resources before the thread terminates.
while (1) {
/* do some work for a while */
/* ... */
/* check if there is a cancellation request */
pthread_testcancel();
}
Conclusion
Thread cancellation allows early termination of threads but requires careful handling to avoid resource leaks and data inconsistency. Deferred cancellation is generally preferred over asynchronous cancellation as it provides safer, more controlled thread termination with proper cleanup opportunities.
