POSIX 线程清理函数

POSIX 多线程的 cleanup 函数

控制清理函数的函数有两个,一个是 pthread_cleanup_push(), 用来把清理函数压入栈中,另外一个是 pthread_cleanup_pop(), 用来把栈中的函数弹出来。linux

用这两个函数组合,能够达到在线程退出时,清理线程数据的做用, 例如对 mutex 进行解锁等。数组

下面是这两个函数的函数原型:多线程

#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void *), void *arg);
void pthread_cleanup_pop(int execute);

//Compile and link with -pthread.

 

咱们先写个简单的例子,感性认识一下这两个函数的做用:函数

#include <stdio.h>
#include <pthread.h>

void handlers(void *arg) {
    if(NULL != arg) {
        printf("%s() : [%s]\n", __func__, (char*)arg);
    } else {
        printf("%s()\n", __func__);
    }
}

void *
thread_start(void *arg) {
    pthread_cleanup_push(handlers, "one");
    pthread_cleanup_push(handlers, "two");
    pthread_cleanup_push(handlers, "thr");
    printf("This is thread [%u]\n", (unsigned int)pthread_self());
    pthread_exit("he~he~");
    //do something 
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);

    return NULL;
}

int main() {
    pthread_t pt;
    pthread_create(&pt, NULL, thread_start, NULL);

    void *r = NULL;
    pthread_join(pt, &r);
    if(NULL != r) {
        printf("thread return : [%s]\n", (const char*)r);
    }

    return 0;
}

 

编译并运行:ui

This is thread [3290769152]
handlers() : [thr]
handlers() : [two]
handlers() : [one]
thread return : [he~he~]

 

咱们在代码里面是按照 one、two、thr 的顺序调用的 pthread_cleanup_push() 函数, 结果在运行后获得的结果中,却看到它们输出的顺序正好倒过来了。 这正是这对函数的性质。spa

而且这对函数还有一个性质,那就是使用 pthread_cleanup_push() 和 pthread_cleanup_pop() 之间使用 return 的话,会致使以后的 pthread_cleanup_pop() 不起做用。 这是为何呢?缘由是,其实 pthread_cleanup_push() 和 pthread_cleanup_pop() 不是函数, 而是一对宏。线程

其宏定义在头文件 pthread.h 中能够看到,宏定义以下:code

#  define pthread_cleanup_push(routine, arg) \
  do {                                        \
    __pthread_cleanup_class __clframe (routine, arg)

#  define pthread_cleanup_pop(execute) \
    __clframe.__setdoit (execute);                        \
  } while (0)

 

咱们写个更简单的程序,把这两个宏展开后看一看是什么样结果:blog

代码以下:get

#  define pthread_cleanup_push(routine, arg) \
  do {                                        \
    __pthread_cleanup_class __clframe (routine, arg)

#  define pthread_cleanup_pop(execute) \
    __clframe.__setdoit (execute);                        \
  } while (0)

 

编译:

gcc -g -E -o pthread_cleanup_macro.i pthread_cleanup_macro.c

 

查看 pthread_cleanup_macro.i 的代码:

void hand(void* arg) {
    printf("do nothing");
}

void *thread_start(void* arg) {
    do { __pthread_unwind_buf_t __cancel_buf; void (*__cancel_routine) (void *) = (hand); void *__cancel_arg = ("a"); int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); if (__builtin_expect((__not_first_call), 0)) { __cancel_routine (__cancel_arg); __pthread_unwind_next (&__cancel_buf); } __pthread_register_cancel (&__cancel_buf); do {;
    printf("This is thread [%u]\n", (unsigned int)pthread_self());
    do { } while (0); } while (0); __pthread_unregister_cancel (&__cancel_buf); if (1) __cancel_routine (__cancel_arg); } while (0);

    return ((void *)0);
}

int main() {
    return 0;
}

 

能够看到,thread_start 函数里面的 pthread_cleanup_push() 和 pthread_cleanup_pop() 已经被展开了。咱们把 thread_start 函数里面的代码再修饰一下格式,结果以下:

void *thread_start(void* arg) {
    do { 
        __pthread_unwind_buf_t __cancel_buf; 
        void (*__cancel_routine) (void *) = (hand); 
        void *__cancel_arg = ("a"); 
        int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); 
        if (__builtin_expect((__not_first_call), 0)) { 
            __cancel_routine (__cancel_arg); 
            __pthread_unwind_next (&__cancel_buf); 
        }
        __pthread_register_cancel (&__cancel_buf);
        do {
            ;
            printf("This is thread [%u]\n", (unsigned int)pthread_self());
            do { 
            } while (0); 
        } while (0); 
        __pthread_unregister_cancel (&__cancel_buf); 
        if (1) __cancel_routine (__cancel_arg); 
    } while (0);

    return ((void *)0);
}

 

能够看到,咱们输出线程信息的 printf 语句,被一层层的 do{}while(0) 给包围了。 若是在 pthread_cleanup_push() 和 pthread_cleanup_pop() 之间加一个 return , 那么整个 do{}while(0) 就会被跳出,后面的代码确定也就不会被执行了。

 

同步地址:https://www.fengbohello.top/archives/linux-pthread-cleanup

相关文章
相关标签/搜索