Question

I've been tasked with creating a user-level thread library that replicates the functionality of pthread (full disclosure: this is for an OS course). However, I'm unclear as to how pthread_join() and pthread_exit() interact when a thread has already exited.

In particular, the man page states:

The pthread_join() function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately.

Is this a guarantee that even if I create millions of threads their results will be kept available for one join? Otherwise, how long can I expect the result to be kept around?

I'm not looking for advice on how to implement it (that's most of the fun!), I'm just trying to nail down the behavior before I dive in.

Was it helpful?

Solution

There's no time limit, the implementation has to keep around the "return value" as long as the process itself is alive to make it available to a join.

The POSIX definition for pthread_exit simply says:

The pthread_exit() function shall terminate the calling thread and make the value value_ptr available to any successful join with the terminating thread.

Doesn't matter whether the join was initiated before the exit, or years later, or whether there was only one additional thread or hundreds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top