首页 > 代码库 > 线程退出前操作

线程退出前操作

#include <unistd.h>#include <pthread.h>#include <stdio.h>#include <stdlib.h>void cleanup(){    printf("cleanup\n");}void *test_cancel(void){    pthread_cleanup_push(cleanup,NULL);    printf("test_cancel\n");    while(1)    {        printf("test message\n");        sleep(1);    }    pthread_cleanup_pop(0);}int main(){    pthread_t tid;    pthread_create(&tid,NULL,(void *(*)(void *))test_cancel,NULL);    sleep(2);    pthread_cancel(tid);    pthread_join(tid,NULL);    return 0;}