如果我在未使用的pthread_t上调用 pthread_join() 会发生什么

What will happen if I call pthread_join() on an unused pthread_t?

本文关键字:pthread 什么 join 未使用 如果 调用      更新时间:2023-10-16

>假设我有一个声明的pthread_t结构,如下所示:

pthread_t newThread;

然后我打电话:

pthread_join(&newThread, NULL, myMethod, NULL);

pthread_join() 会做什么?

根据 ISO C,newThread变量是一个"不确定值的对象",使用它会触发未定义的行为。它可能有一个触发 CPU 异常的"陷阱表示形式"。

或者它可能只是被解释为该类型的随机值,API 可以通过以下两种方式之一处理该值:要么没有这样的线程,然后返回ESRCH,要么侥幸有这样的线程。然后出现了各种情况:它是否可以加入,等等。

如果将pthread_join称为未初始化的pthread_t,则行为是未定义的。