pthread_join中的悬空指针问题?

3
根据这个链接,pthread_join将输出参数**thread_return设置为pd->result,然后释放pd。这样做是否正确?我是否遗漏了什么,或者glibc中存在严重的错误?
    /* We mark the thread as terminated and as joined.  */
    pd->tid = -1;

    /* Store the return value if the caller is interested.  */
    if (thread_return != NULL)
      *thread_return = pd->result;


    /* Free the TCB.  */
    __free_tcb (pd);

1
请提供一个最小的、可编译的测试用例。 - autistic
1个回答

1

__free_tbc 不释放 pd,而是线程的堆栈,即 pd->tpp(也请参见 这里)。因此,在这些语句之后,pd->result 仍然是一个有效的指针。


谢谢。还有一件事让我感到困扰,指针可能指向堆栈,但是手册明确警告不要这样做:由retval指向的值不应位于调用线程的堆栈上,因为该堆栈的内容在线程终止后是未定义的。(- 因此线程应使用malloc。) - danuker
@danuker:retval 指向的值可以是全局变量或堆上分配的,因为 pthread_join 删除了栈。man 手册是正确的。 - md5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接