I was asked from where do we know that when passing NULL as a second argument in pthread_create() function the thread is made joinable.
I mean, I know that man pages state so, but a justification in code was demanded.
I know that when NULL is passed in, default attributes are used:
const struct pthread_attr *iattr = (struct pthread_attr *) attr;
if (iattr == NULL)
/* Is this the best idea? On NUMA machines this could mean accessing far-away memory. */
iattr = &default_attr;
I know that it should be somewhere in the code of pthread library, but I don’t know where exactly.
I know that the definition of default_attr is in pthread_create.c:
static const struct pthread_attr default_attr = { /* Just some value > 0 which gets rounded to the nearest page size. */ .guardsize = 1, };
but I do not know where is exactly stated in the code that this result in a joinable thread.
Thanks in advance.
First off, from the code you pasted you can see that
default_attrcontains zeroes in almost all fields (there’s no such thing as a half-initialized variable in C: if you only initialize some fields, the others are set to 0).Second,
pthread_createcontains this code:This line checks whether
iattr->flagshas theATTR_FLAG_DETACHSTATEbit set, which (fordefault_attr) it doesn’t becausedefault_attr.flagsis 0. Thus it setspd->joinidtoNULLand not topdas for detached threads.(Note that this answer only applies to GNU glibc and not to POSIX pthreads in general.)