加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为已经退出的线程调用pthread_detach?

发布时间:2020-12-16 09:21:33 所属栏目:百科 来源:网络整理
导读:#include pthread.h#include unistd.hstatic void *tfunc(void *data){ return NULL;}int main(int argc,char **argv){ pthread_t t; pthread_create(t,NULL,tfunc,NULL); sleep(1); pthread_detach(t); return 0;} 参见MWE. 它工作正常,但我不确定这是否是
#include <pthread.h>
#include <unistd.h>

static void *tfunc(void *data)
{
    return NULL;
}

int main(int argc,char **argv)
{
    pthread_t t;

    pthread_create(&t,NULL,tfunc,NULL);
    sleep(1);
    pthread_detach(t);

    return 0;
}

参见MWE.
它工作正常,但我不确定这是否是实际定义的行为. pthread_detach的手册页没有说明在退出的线程上调用它.

是的我知道创建具有分离属性的线程,但我对这种情况特别好奇. pthread_join在这个案例中有提及,我认为pthread_detach的工作原理一样好,但我没有找到任何官方声明.

解决方法

此代码完全合法,并且不会调用未定义的行为:

#include <pthread.h>
#include <unistd.h>

static void *tfunc(void *data)
{
    return NULL;
}

int main(int argc,NULL);
    sleep(1);
    pthread_detach(t);

    return 0;
}

它没有明确说明,但是POSIX documentation for pthread_detach()的措辞是这样的,它必须被定义并且在终止线程上调用pthread_detach()是正确的:

The pthread_detach() function shall indicate to the implementation
that storage for the thread thread can be reclaimed when that thread
terminates. If thread has not terminated,pthread_detach() shall not
cause it to terminate.

The behavior is undefined if the value specified by the thread
argument to pthread_detach() does not refer to a joinable thread.

首先,请注意语句“如果线程尚未终止”.这意味着当线程终止时调用pthread_detach()必须是安全的.

其次,请注意“如果……不引用可连接线程,则行为未定义”.在您发布的代码中,您创建的线程显然是可连接的 – 您没有使用分离属性创建它,因此您可以调用pthread_join()来检索其返回值.所以这不是未定义的行为.

请记住,当调用pthread_join()或pthread_detach()时,无法保证从线程A确保线程B仍在运行.因此,要么从任何其他线程上的任何线程调用(一次!),要么是安全的.

另外,从POSIX文档的Rationale部分:

RATIONALE

The pthread_join() or pthread_detach() functions should eventually
be called for every thread that is created so that storage associated
with the thread may be reclaimed.

It has been suggested that a “detach” function is not necessary; the
detachstate thread creation attribute is sufficient,since a thread
need never be dynamically detached. However,need arises in at least
two cases:

  1. In a cancellation handler for a pthread_join() it is nearly essential to have a pthread_detach() function in order to detach the
    thread on which pthread_join() was waiting. Without it,it would be
    necessary to have the handler do another pthread_join() to attempt
    to detach the thread,which would both delay the cancellation
    processing for an unbounded period and introduce a new call to
    pthread_join(),which might itself need a cancellation handler. A
    dynamic detach is nearly essential in this case.

  2. In order to detach the “initial thread” (as may be desirable in processes that set up server threads).

同样,虽然没有明确说明,但请注意pthread_join()和pthread_detach()之间的隐含等价.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读