POSIX Threads

POSIX Threads

To make it possible to write portable threaded programs, IEEE has described a standard for threads in IEEE standard 1003.1c. The threads package it describes is called Pthreads. Most UNIX systems support it. The standard defines over 60 function calls, which is far too many to go over here. Instead we will just explain a few of the most important ones to give an idea of how it works. The calls we will explain are listed in figure 1.

All Pthreads threads have specific characteristics. Each one has an identifier, a set of registers (including the program counter), and a set of attributes, which are stored in a structure. The attributes include the stack size, scheduling parameters, and other items required to use the thread.

Some of the Pthreads function calls

A new thread is created using the pthread_create call. The thread identifier of the newly created thread is returned as the function value. This call is intentionally very much like the fork system call, with the thread identifier playing the role of the PID, mostly for identifying threads referenced in other calls. When a thread has finished the work it has been assigned, it can stop by calling pthread_exit. This call stops the thread and releases its stack. Sometimes a thread needs to wait for another thread to finish its work and exit before continuing. The thread that is waiting calls pthread_join to wait for a particular other thread to stop. The thread identifier of the thread to wait for is given as a parameter.

Often it happens that a thread is not rationally blocked, but feels that it has run long enough and wants to give another thread a chance to run. It can achieve this goal by calling pthread_yield. There is no such call for processes because the theory there is that processes are fiercely competitive and each one wants all the CPU time it can get. However, since the threads of a process are working jointly and their code is invariably written by the same programmer, sometimes the programmer wants them to give each other upa chance.

The next two thread calls deal with attributes. Pthread_attr _init creates the attribute structure linked with a thread and initializes it to the default values. These values (such as the priority) can be altered by managing fields in the attribute structure. Finally, pthread_attr _destroy removes a thread's attribute structure, freeing up its memory. It does not affect threads using it; they continue to exist.

To get a better feel for how Pthreads works, examine the simple example of figure 2. Here the main program loops NUMBER_OF_THREADS times, creating a new thread on each iteration, after announcing its purpose. If the thread creation fails, it prints an error message and then exits. After creating all the threads, the main program exits. When a thread is created, it prints a one-line message announcing itself, then it exits. The order in which the various messages are interleaved is nondeterminate and may vary on consecutive runs of the program.

An example program using threads

The Pthreads calls explained above are not the only ones by any means; there are many more. We will look at some of the others later after we have discussed process and thread synchronization.


attributes, process, pthreads