How long does the `pthread_t*` argument to `pthread_create` need to survive?

Viewed 424

The signature of pthread_create(3) is:

   int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

What are the requirements on the storage duration of the pthread_t *thread argument? The man page for pthread_create says:

Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread;

But it's not clear if it means that it stores the value there so that the caller can examine it, or if it uses that buffer to store the value (implying that the buffer needs to stay usable for the whole lifetime of the child thread).

Similarly, pthread_self says it returns

the same value that is returned in *thread in the pthread_create(3) call that created this thread.

but it's not clear if it means that it returns the value stored in *thread or a value equal to what was returned in *thread.

Concretely, I'm wondering if it's legal to have something like:

void make_thread(void) {
  pthread_t tid;
  return pthread_create(&tid, NULL, some_fn, NULL);
}

or if tid needs to be malloc'd. When I put tid on the stack, I get a bunch of errors in valgrind related to _Unwind_ForcedUnwind, which makes me suspect that *thread needs to remain valid for the lifetime of the child thread.

1 Answers
Related