How does pthread_create() work?

c, c++, pthreads

Solution

What exactly does pthread_create do to thread?

`thread` is an object, it can hold a value to identify a thread. If `pthread_create` succeeds, it fills in a value that identifies the newly-created thread. If it fails, then the value of `thread` after the call is undefined. (reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html)

What happens to thread after it has joined the main thread and terminated?

Nothing happens to the object, but the value it holds no longer refers to any thread (so for example you can no longer pass it to functions that take a `pthread_t`, and if you accidentally do then you might get `ESRCH` errors back).

What happens if, after thread has joined, you do this:

Same as before: if `pthread_create` succeeds, a value is assigned that identifies the newly-created thread.

Problem

Given the following: ``` pthread_t thread; pthread_create(&thread, NULL, function, NULL); ``` What exactly does `pthread_create` do to `thread`? What happens to `thread` after it has joined the main thread and terminated? What happens if, after `thread` has joined, you do this: ``` pthread_create(&thread, NULL, another_function, NULL); ```

Original source