A Few Function Calls for POSIX Threads

Here are some notes on a few functions from the pthread library. In each case, there is more to say that what is mentioned here. A good place to find more information is linux.die.net.

In general, the various threads of a process share the same address space, including code and data. Each thread has its own set of registers and its own stack for function calls.

There is no concept of parent and child with threads.

Each of these functions is in the library:

     #include <pthread.h>

Each should be compiled and linked with -pthread.


pthread_create()

Prototype:

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

Purpose

This function starts a new thread in the calling process. The function executing in the new thread will be start_routine, and arg is passed as the one and only argument of start_routine. The ID of the new thread is stored in *thread. The argument attr points to a structure which describes the attributes of the new thread.

Notes


pthread_exit()

Prototype: void pthread_exit(void * retval);

Purpose: This function terminates the calling thread. A return value is returned in *retval, and this value is then available to another thread of the same process that calls pthread_join().

Notes:


pthread_join()

Prototype: int pthread_join(pthread_t thread, void * * retval);

Purpose: This function waits for the thread specified by thread to terminate. (If that thread has already terminated, pthread_join() will return at once.) If retval is not NULL, the exit value of the terminated thread will be copied into the location whose address is in *retval.

Notes: