Thread Cancellation is a way where it's necessary sometimes to terminate a thread explicitly from another thread.
In Windows we have a Function, it's TerminateThread()
The function signature:
BOOL WINAPI TerminateThread(
_Inout_ HANDLE hThread,
_In_ DWORD dwExitCode
);
This function is always asynchronous. It never guarantees that the thread will always terminate by the time the function returns. This function never cleans up a stack of destroyed threads. So, it's not recommended to use this function until it's absolutely necessary.
Note: As mentioned by Jeffrey Richter, Microsoft designed this API purposely implemented this way to terminate thread(s). This ensures that other threads are still running and accessing the terminated threads stack will not get an access violation. So, by leaving the terminated threads stack in memory, other threads which are accessing the stack of the killed thread can continue to execute.
In Linux we have a function, it's pthread_cancel()
The function signature:
int pthread_cancel(pthread_t thread);
A thread may be in one of the three states with respect to cancellation.
a. Asynchronously cancellable - It can be canceled at any point in time in its execution. pthread_setcanceltype() is used to set thread cancellation asynchronous through PTHREAD_CANCEL_ASYNCHRONOUS. However, this is not recommended.
b. Synchronously cancellable - In this case cancellation occurs when it reaches a specific point in its execution. These places are called cancellation points.
c. Thread also can be uncancellable by setting PTHREAD_CANCEL_DISABLE through pthread_setcancelstate(). An attempt to cancel the thread is ignored.
Thread cancellation in Linux this way may lead to resource leak if cancellation occurs in between but before resource de-allocation. We'll see how this can be handled in the thread to avoid resource leaks in the next article. However, destructors for automatic objects on the stack are run when a thread is canceled. The stack is unwound and the destructors are run in reverse order.
In Windows we have a Function, it's TerminateThread()
The function signature:
BOOL WINAPI TerminateThread(
_Inout_ HANDLE hThread,
_In_ DWORD dwExitCode
);
This function is always asynchronous. It never guarantees that the thread will always terminate by the time the function returns. This function never cleans up a stack of destroyed threads. So, it's not recommended to use this function until it's absolutely necessary.
Note: As mentioned by Jeffrey Richter, Microsoft designed this API purposely implemented this way to terminate thread(s). This ensures that other threads are still running and accessing the terminated threads stack will not get an access violation. So, by leaving the terminated threads stack in memory, other threads which are accessing the stack of the killed thread can continue to execute.
In Linux we have a function, it's pthread_cancel()
The function signature:
int pthread_cancel(pthread_t thread);
A thread may be in one of the three states with respect to cancellation.
a. Asynchronously cancellable - It can be canceled at any point in time in its execution. pthread_setcanceltype() is used to set thread cancellation asynchronous through PTHREAD_CANCEL_ASYNCHRONOUS. However, this is not recommended.
b. Synchronously cancellable - In this case cancellation occurs when it reaches a specific point in its execution. These places are called cancellation points.
c. Thread also can be uncancellable by setting PTHREAD_CANCEL_DISABLE through pthread_setcancelstate(). An attempt to cancel the thread is ignored.
Thread cancellation in Linux this way may lead to resource leak if cancellation occurs in between but before resource de-allocation. We'll see how this can be handled in the thread to avoid resource leaks in the next article. However, destructors for automatic objects on the stack are run when a thread is canceled. The stack is unwound and the destructors are run in reverse order.
Comments