Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

pthreads

Suppose I have a declared pthread_t struct, like the following:

pthread_t newThread;

And then I call:

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

What will pthread_join() do?

like image 387
Groppe Avatar asked Sep 01 '25 03:09

Groppe


1 Answers

According to ISO C, the newThread variable is an "indeterminately valued object", the use of which triggers undefined behavior. It could have a "trap representation" which triggers a CPU exception.

Or it may just be interpreted as a random value of that type, which the API could handle in one of two ways: either there is no such thread, and ESRCH is returned, or by fluke there is such a thread. Then various cases arise: is it joinable or not, etc.

like image 148
Kaz Avatar answered Sep 02 '25 17:09

Kaz