1.pthread_exit终止线程
#include <pthread.h>
void pthread_exit(void *retval);
功能:终止一个线程,在哪个线程中调用,就表示终止哪个线程
参数:
retval:需要传递一个指针,作为一个返回值,可以在pthread_join()中获取到。
pthread_t pthread_self(void);
功能:获取当前的线程的线程ID
int pthread_equal(pthread_t t1, pthread_t t2);
功能:比较两个线程ID是否相等
不同的操作系统,pthread_t类型的实现不一样,有的是无符号的长整型,有的
是使用结构体去实现的。
因此我们在比较线程号时,不应该使用等号而是使用pthread_equal
当一个进程中的所有线程退出时进程才退出
2.pthread_join连接终止进程进行回收
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
- 功能:和一个已经终止的线程进行连接
回收子线程的资源
这个函数是阻塞函数,调用一次只能回收一个子线程
一般在主线程中使用
- 参数:
- thread:需要回收的子线程的ID
- retval: 接收子线程退出时的返回值
- 返回值:
0 : 成功
非0 : 失败,返回的错误号
由于pthread_join函数是一个阻塞函数,主线程需要一直阻塞等待回收终止的子线程。
如果我们不想让主线程始终等待,可以考虑使用进程分离
3.pthread_detach分离一个进程
#include <pthread.h>
int pthread_detach(pthread_t thread);
- 功能:分离一个线程。被分离的线程在终止的时候,会自动释放资源返回给系统。
1.不能多次分离,会产生不可预料的行为。
2.不能去连接一个已经分离的线程,会报错。
- 参数:需要分离的线程的ID
- 返回值:
成功:0
失败:返回错误号
注意:不能多次分离,不能连接已经分离的线程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
printf("chid thread id : %ld\n", pthread_self());
pthread_exit(NULL);
}
int main() {
// 创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error1 : %s\n", errstr);
}
// 输出主线程和子线程的id
printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
// 设置子线程分离,子线程分离后,子线程结束时对应的资源就不需要主线程释放
ret = pthread_detach(tid);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error2 : %s\n", errstr);
}
pthread_exit(NULL);
return 0;
}
小问题:如果一个线程先于detach()结束,或者还没运行到join()时,他的形式是一个“僵尸”线程
4.pthread_cancel线程取消
#include <pthread.h>
int pthread_cancel(pthread_t thread);
- 功能:取消线程(让线程终止)
取消某个线程,可以终止某个线程的运行,
但是并不是立马终止,而是当子线程执行到一个取消点,线程才会终止。
取消点:系统规定好的一些系统调用,我们可以粗略的理解为从用户区到内核区的
切换,这个位置称之为取消点。
一些常见的取消点:
- I/O 操作:例如 read()、write()、recv() 和 send() 等系统调用
- 信号处理函数:当进城接收到某些信号时(例如 SIGINT 或者 SIGTERM),其相关处理函数将成为一个可选的取消点
- 锁和条件变量操作:例如 pthread_mutex_lock(), pthread_cond_wait(), sem_wait()
- 动态内存分配和释放函数:例如 malloc(), calloc(), realloc(), free()
void * callback(void * arg) {
printf("chid thread id : %ld\n", pthread_self());
for(int i = 0; i < 5; i++) {
printf("child : %d\n", i);
}
return NULL;
}
我们给出的callback函数中,打印是一种IO操作,因此是一个取消点,运行时在打印任何数时取消都有可能
另外:如果在主线程中设置了子线程的线程取消但是callback中并没有取消点,那么子线程会运行完剩余代码再终止。