Understanding of pthread_cancel function and cancellation point
1. What does the pthread_cancel function do
The pyhread_cancel function just sends a request to the thread. The request is to terminate the thread.
So for this request, it is just a suggestion for threads
The thread may not terminate immediately, and will continue to run until it reaches the cancellation point.
2. Cancel the understanding
The cancellation point is a place where the thread checks whether it is cancelled
How does the cancellation point appear?
For cancellation points, there will be cancellation points for certain functions
For example: sleep, wait, waitpid, waitid, send and other functions
Figure: These functions can be used as cancellation points
3.pthread_setcancelstate
This function can set the cancel state, there are two states PTHREAD_CANCEL_ENABLE (cancelable state) and PTHREAD_CANCEL_DISABLE (uncancelable state).
#include <pthread.h> pthread_setcancelstate(int state, int* oldstate); parameter: state: Change the current state to state oldstate: put the original state of the thread into the space pointed to by oldtype return value: Return 0 on success, error code on failureThese two steps are an atomic operation.
For the pthread_cancel function, the default is PTHREAD_CANCEL_ENABLE to cancel the state . , When the state is set to PTHREAD_CANCEL_DISABLE , the call to pthread_cancel will not kill the thread . Rather, the cancellation request for this thread is still in a suspended state (i.e. pending ) until the thread is canceled state to PTHREAD_CANCEL_ENABLE , the thread will process all of the pending requests on the next cancellation point .
4. pthread_setcanceltype
Set the type of cancellation.
#include <thread.h> int pthread_testcanceltype(int type, int* oldtype); parameter: type: Set the type of cancellation to type oldtype: put the cancellation type of the thread in the space pointed to by oldtype return value: Return 0 on success, error code on failure
Our default cancellation type is postponed cancellation. That is, it will run to the cancellation point and then cancel
There are PTHREAD_CANCEL_DEFERRED (deferred cancellation) for the cancellation type that can be set, which is the default cancellation type.
PTHRAED_CANCEL_ASYNCHRONOUS (asynchronous cancellation), after adopting asynchronous cancellation, the thread can be cancelled at any time, and it cannot be cancelled without getting the cancellation point.
5. pthread_testcancel
For a thread that does not call the above-mentioned function that can generate a cancellation point, then the thread has no cancellation point and cannot be cancelled, and the cancellation request will always be suspended and will not be processed by the thread.
So if you want to cancel a thread, you can add a cancellation point to the thread yourself
The pthread_testcancel function can add a cancellation point to the program itself.
#include <thread.h> void pthread_testcancel(void);Call the pthread_testcancel function, if a cancellation request is in a pending state, and the cancellation is not set to invalid, then the thread will be cancelled immediately. However, if the cancellation is set to invalid, then the pthread_testcancel function call has no effect .