pthread_key_create() - 什么时候调用`析构函数`函数?

Eri*_*ang 3 c linux multithreading pthreads

我编写了一个程序来了解Linux(Linux 3.13.0-24-generic #46-Ubuntu)中特定于线程的数据,如下所示.

我尝试在传递给析构函数的函数中打印线程id pthread_key_create(),但似乎只有子线程成功打印,但主线程没有打印该信息.

我的问题是:

在线程终止之前或之后是否正确调用析构函数?

主线程没有打印信息的原因是,由于主线程已被破坏?

tsd_test.c

// test of thread-specific data

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>

static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_key_t tidKey;

static void destructor(void *buf) {
    unsigned long *_tid = buf;
    printf("destroy, tid: %lu\n", *_tid);
    free(buf);
}

static void createKey(void) {
    int s = pthread_key_create(&tidKey, destructor);
    if(s != 0) {
        printf("failed to create key\n");
        exit(-1);
    }
}

void *store_tid() {
    int s;
    unsigned long *buf;

    // create key
    s = pthread_once(&once, createKey);
    if(s != 0) {
        printf("failed to create key\n");
        exit(-1);
    }

    buf = pthread_getspecific(tidKey);
    if(buf == NULL) { // thread call this function for the first time,
        buf = malloc(sizeof(unsigned long));
        if(buf == NULL) {
            printf("failed to allocate memory, %s\n", strerror(errno));
            exit(-1);
        }
        // register buffer to specified key & current thread,
        s = pthread_setspecific(tidKey, buf);
        if(s != 0) {
            printf("failed to setspecific\n");
            exit(-1);
        }
    }

    // store tid to buffer,
    *buf = (unsigned long)pthread_self();
    printf("set tid to: %lu\n", *buf);

    return buf;
}

void tsd_test() {
    unsigned long *tidp_a = store_tid();
    printf("tid - before call another thread: %lu\n", *tidp_a);

    int s;
    pthread_t t2;
    s = pthread_create(&t2, NULL, &store_tid, NULL);
    if(s != 0) {
        printf("failed to create thread\n");
        exit(-1);
    }

    s = pthread_join(t2, NULL);
    if(s != 0) {
        printf("failed to join thread\n");
        exit(-1);
    }

    printf("tid - after call another thread: %lu\n", *tidp_a);
}

int main(int argc, char *argv[]) {
    tsd_test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

gcc -pthread tsd_test.c
Run Code Online (Sandbox Code Playgroud)

输出:

set tid to: 3076318976
tid - before call another thread: 3076318976
set tid to: 3076315968
destroy, tid: 3076315968
tid - after call another thread: 3076318976
Run Code Online (Sandbox Code Playgroud)

你可以看到只有子线程打印"destroy",而主线程没有.

P.P*_*.P. 5

线程退出时调用线程析构函数,而不是在进程终止时main(),即退出时,整个进程终止.所以析构函数不会被调用.

打电话 pthread_exit(NULL);或者在结束main()函数或结束tst()函数(都是相同真的).现在,您将看到被调用的析构函数.