线程退出前操作

#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;
}

posted on 2014-06-15 11:47  lakeone  阅读(185)  评论(0编辑  收藏  举报

导航