C 如果对';他已经回来了
对于以下代码,如果在调用“C 如果对';他已经回来了,c,linux,pthreads,C,Linux,Pthreads,对于以下代码,如果在调用“pthread\u detach(thread\u Id)”之前,所创建的线程“thread\u Id”返回/完成其工作”某些函数“”,那么预期的行为是什么?“thread\u Id”使用的资源是否会被释放 pthread_create(&thread_Id,NULL,some_函数,&queue); pthread_detach(线程Id) //否pthread_join(thread_Id,…)是将自动释放已完成线程的资源 根据Linux的pthread_deta
pthread\u detach(thread\u Id)
”之前,所创建的线程“thread\u Id
”返回/完成其工作”某些函数“
”,那么预期的行为是什么?“thread\u Id
”使用的资源是否会被释放
pthread_create(&thread_Id,NULL,some_函数,&queue);
pthread_detach(线程Id)
//否pthread_join(thread_Id,…)是将自动释放已完成线程的资源 根据Linux的
pthread_detach()
手册,它失败的原因只有两个:
EINVAL - thread is not a joinable thread.
ESRCH - No thread with the ID thread could be found.
在生命周期已结束的线程上调用
pthread\u detach
:
如果应用程序尝试使用其生存期已结束的线程ID,则该行为未定义。“
但是实现pthread\u detach
的关键是:
如果实现在其生命周期结束后检测到线程ID的使用,建议函数失败并报告[ESRCH]错误
难以阅读。尝试改进如果存在这种风险,最好使用
pthread\u attr\u t*
而不是NULL,并设置线程属性,以便线程开始处于分离状态。请参阅:和。我猜pthread\u detach()
将报告失败,线程状态可能不会被清除。[…继续…][…继续…]但是,的规范说明:pthread_detach()函数应向实现指示,当线程终止时,可以回收该线程的存储。如果线程尚未终止,则pthread_detach()不应导致其终止。但是,下面的理由是:如果实现在其生命周期结束后检测到线程ID的使用,建议函数失败并报告[ESRCH]错误。[…续2…][…续2…]这些基本原理注释意味着,如果线程在可以分离之前终止,那么资源将丢失(除非您决定pthread\u join()
ESRCH失败时的线程ID)。