线程可以安排他退出时需要调用的函数,这与进程可以用atexit函数安排进程退出时需要调用的函数是类似的。这样的函数称为线程清理处理程序,线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说他们的执行顺序与他们注册的顺序相反。
1.线程退出清理函数
#include <pthread.h>
//将清理函数压栈
/*
参数:
rtn:线程清理函数指针
arg:传给清理函数的参数
*/
void pthread_cleanup_push(void (*rtn)(void *), void *arg);
//将清理函数出栈
/*
参数:
execute:清理函数执行标志位
*/
void pthread_cleanup_pop(int execute);
当线程执行以下动作会自动调用清理函数: - 调用pthread_exit退出线程 - 响应其它线程的取消请求 - 用非零execute调用pthread_cleanup_pop
实例: 线程退出时调用清理函数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
void mycleanup(void *arg)
{
printf("clean up arg=%s\n", (char *)arg);
free((char *)arg);
}
void *thr_fn(void *arg)
{
char *pt = NULL;
printf("son thread is running...\n");
pt =(char*)malloc(100);
pthread_cleanup_push(mycleanup,(void*)(pt));
bzero(pt,100);
strcpy(pt,"memory from malloc..");
printf("before exit...\n");
sleep(3);
pthread_exit(NULL); //线程退出时会自动调用清理函数
printf("before pop...");
pthread_cleanup_pop(1); //有push就一定要有pop
}
int main(int argc, char const *argv[])
{
printf("main thread is running....\n");
pthread_t thread;
if (pthread_create(&thread, NULL, thr_fn, NULL) != 0)
{
perror("error to pthread_create:");
exit(-1);
}
if (pthread_join(thread, NULL) != 0)
{
perror("error to join:");
exit(-1);
}
printf("main thread will exit....\n");
return 0;
}
执行结果:
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./a.out
main thread is running....
son thread is running...
before exit...
clean up arg=memory from malloc..
main thread will exit....
线程取消时执行清理函数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
void mycleanup(void *arg)
{
printf("clean up arg=%s\n", (char *)arg);
free((char *)arg);
}
void *thr_fn(void *arg)
{
char *pt = NULL;
printf("son thread is running...\n");
pt =(char*)malloc(100);
pthread_cleanup_push(mycleanup,(void*)(pt));
bzero(pt,100);
strcpy(pt,"memory from malloc..");
printf("before exit...\n");
sleep(10);
printf("before pop...");
pthread_cleanup_pop(1); //有push就一定要有pop
return NULL;
}
int main(int argc, char const *argv[])
{
printf("main thread is running....\n");
pthread_t thread;
if (pthread_create(&thread, NULL, thr_fn, NULL) != 0)
{
perror("error to pthread_create:");
exit(-1);
}
sleep(5);
//5秒后取消子线程
pthread_cancel(thread);
if (pthread_join(thread, NULL) != 0)
{
perror("error to join:");
exit(-1);
}
printf("main thread will exit....\n");
return 0;
}
执行结果:
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./a.out
main thread is running....
son thread is running...
before exit...
clean up arg=memory from malloc..
main thread will exit....
调用pthread_cleanup_pop时执行清理函数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
void mycleanup_fn1(void *arg)
{
printf("clean up fn1 arg=%s\n", (char *)arg);
free((char *)arg);
}
void mycleanup_fn2(void *arg)
{
printf("clean up fn2... \n");
}
void *thr_fn(void *arg)
{
char *pt = NULL;
printf("son thread is running...\n");
pt =(char*)malloc(100);
pthread_cleanup_push(mycleanup_fn1,(void*)(pt));
pthread_cleanup_push(mycleanup_fn2,NULL);
bzero(pt,100);
strcpy(pt,"memory from malloc..");
printf("before exit...\n");
sleep(3);
//注意:pop必须与push的数量匹配
printf("before pop...\n");
pthread_cleanup_pop(1); //有push就一定要有pop
printf("before pop...\n");
pthread_cleanup_pop(1); //有push就一定要有pop
return NULL;
}
int main(int argc, char const *argv[])
{
printf("main thread is running....\n");
pthread_t thread;
if (pthread_create(&thread, NULL, thr_fn, NULL) != 0)
{
perror("error to pthread_create:");
exit(-1);
}
if (pthread_join(thread, NULL) != 0)
{
perror("error to join:");
exit(-1);
}
printf("main thread will exit....\n");
return 0;
}
执行结果:
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./a.out
main thread is running....
son thread is running...
before exit...
before pop...
clean up fn2...
before pop...
clean up fn1 arg=memory from malloc..
main thread will exit....