Fork me on GitHub

The Pthreads API

The Pthreads API

The original Pthreads API was defined in the ANSI/IEEE POSIX 1003.1 - 1995 standard. The POSIX standard has continued to evolve and undergo revisions, including the Pthreads specification.

Copies of the standard can be purchased from IEEE or downloaded for free from other sites online.

The subroutines which comprise the Pthreads API can be informally grouped into four major groups:

Naming conventions: All identifiers in the threads library begin with pthread_. Some examples are shown below.

Routine PrefixFunctional Group
pthread_Threads themselves and miscellaneous subroutines
pthread_attr_Thread attributes objects
pthread_mutex_Mutexes
pthread_mutexattr_Mutex attributes objects.
pthread_cond_Condition variables
pthread_condattr_Condition attributes objects
pthread_key_Thread-specific data keys
pthread_rwlock_Read/write locks
pthread_barrier_Synchronization barriers

The concept of opaque objects pervades the design of the API. The basic calls work to create or modify opaque objects - the opaque objects can be modified by calls to attribute functions, which deal with opaque attributes.

The Pthreads API contains around 100 subroutines. This tutorial will focus on a subset of these - specifically, those which are most likely to be immediately useful to the beginning Pthreads programmer.

For portability, the pthread.h header file should be included in each source file using the Pthreads library.

The current POSIX standard is defined only for the C language. Fortran programmers can use wrappers around C function calls. Some Fortran compilers may provide a Fortran pthreads API.

A number of excellent books about Pthreads are available. Several of these are listed in the References section of this tutorial.