pthread_cancel(): cancel thread
pthread_setcancelstate(): Set whether to allow cancellation
pthread_setcanceltype(): Set the cancellation method, asynchronous or postponed
pthread_testcancel(): set a cancellation point
pthread_detach(): thread detach
The pthread_join() thread collecting corpses mentioned above must wait for the thread execution to finish before collecting the corpses, and sometimes it does not need the target thread to finish executing, so how to collect the corpses?
pthread_cancel() + pthread_join()
pthread_cancel(): cancel thread
NAME
pthread_cancel - send a cancellation request to a thread 给一个线程发送一个取消请求。
SYNOPSIS
#include <pthread.h>
int pthread_cancel(pthread_t thread);//取消线程, thread :需要取消的线程ID
Compile and link with -pthread.
In order to prevent the resources that have been applied for cannot be recycled, the cancellation status needs to be set. For example, if the thread that needs to be cancelled has just opened () a file, canceling at this time will result in missed close() and resource leakage. So you need to set the following status as follows:
There are two states of cancellation:
1 Cancellation allowed
1.1 异步取消
1.2 推迟取消,默认是推迟取消, 推迟到 cancel点 再响应
Cancel point: The cancel point defined by POSIX refers to system calls that may cause blocking.
2 Cancellation is not allowed
int pthread_setcancelstate(int state, int *oldstate); // Set whether to allow cancellation
int pthread_setcanceltype(int type, int *oldtype);// Set the cancellation method, asynchronous or postponed
NAME
pthread_setcancelstate, pthread_setcanceltype - set cancelability state and type
SYNOPSIS
#include <pthread.h>
// 设置 是否允许取消
int pthread_setcancelstate(int state, int *oldstate);
// 设置取消方式,异步 或者 推迟
int pthread_setcanceltype(int type, int *oldtype);
Compile and link with -pthread.
NAME
pthread_testcancel-set a cancellation point
Applied to the function function without any system call that may cause blocking, that is, in the thread, it is used to set the cancellation point to cancel the thread
SYNOPSIS
#include <pthread.h>
// 本函数 什么都不做,就是一个取消点
void pthread_testcancel(void);
Compile and link with -pthread.
pthread_detach(): thread detach
Under normal circumstances, we always follow the principles of linux resources: who opens them, who closes them. Whoever applies, who releases this principle. But there are special circumstances. If a thread is created and no longer cares about its life or death, it can be detached, that is,
pthread_detach().
Note that threads that have been detached by pthread_detach() cannot be collected by pthread_join()
NAME
pthread_detach - detach a thread 分离一个线程
SYNOPSIS
#include <pthread.h>
int pthread_detach(pthread_t thread);
Compile and link with -pthread.