POSIX Threads Programming

Author: Blaise Barney, Lawrence Livermore National Laboratory UCRL-MI-133316

Table of Contents

  1. Abstract
  2. Pthreads Overview
    1. What is a Thread?
    2. What are Pthreads?
    3. Why Pthreads?
    4. Designing Threaded Programs
  3. The Pthreads API
  4. Compiling Threaded Programs
  5. Thread Management
    1. Creating and Terminating Threads
    2. Passing Arguments to Threads
    3. Joining and Detaching Threads
    4. Stack Management
    5. Miscellaneous Routines
  6. Exercise 1
  7. Mutex Variables
    1. Mutex Variables Overview
    2. Creating and Destroying Mutexes
    3. Locking and Unlocking Mutexes
  8. Condition Variables
    1. Condition Variables Overview
    2. Creating and Destroying Condition Variables
    3. Waiting and Signaling on Condition Variables
  9. Monitoring, Debugging and Performance Analysis Tools for Pthreads
  10. LLNL Specific Information and Recommendations
  11. Topics Not Covered
  12. Exercise 2
  13. References and More Information
  14. Appendix A: Pthread Library Routines Reference


In shared memory multiprocessor architectures, threads can be used to implement parallelism. Historically, hardware vendors have implemented their own proprietary versions of threads, making portability a concern for software developers. For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads.

The tutorial begins with an introduction to concepts, motivations, and design considerations for using Pthreads. Each of the three major classes of routines in the Pthreads API are then covered: Thread Management, Mutex Variables, and Condition Variables. Example codes are used throughout to demonstrate how to use most of the Pthreads routines needed by a new Pthreads programmer. The tutorial concludes with a discussion of LLNL specifics and how to mix MPI with pthreads. A lab exercise, with numerous example codes (C Language) is also included.

Level/Prerequisites: This tutorial is ideal for those who are new to parallel programming with pthreads. A basic understanding of parallel programming in C is required. For those who are unfamiliar with Parallel Programming in general, the material covered in EC3500: Introduction to Parallel Computing would be helpful.

Pthreads Overview

What is a Thread?

Pthreads Overview

What are Pthreads?

Pthreads Overview

Why Pthreads?

Light Weight:

Efficient Communications/Data Exchange:

Other Common Reasons:

Pthreads Overview

Designing Threaded Programs

Parallel Programming:

Shared Memory Model:


Thread Limits:

The Pthreads API

Compiling Threaded Programs

Thread Management

Creating and Terminating Threads


Creating Threads:

Thread Attributes:

Thread Binding and Scheduling:

Question: After a thread has been created, how do you know a)when it will be scheduled to run by the operating system, and b)which processor/core it will run on?

Terminating Threads & pthread_exit():

Example: Pthread Creation and Termination

Thread Management

Passing Arguments to Threads

Question: How can you safely pass data to newly created threads, given their non-deterministic start-up and scheduling?

Thread Management

Joining and Detaching Threads



Joinable or Not?



Example: Pthread Joining

Thread Management

Stack Management


Preventing Stack Problems:

Some Practical Examples at LC:

Example: Stack Management

Thread Management

Miscellaneous Routines

Pthread Exercise 1

Getting Started and Thread Management Routines

  • Login to an LC cluster using your workshop username and OTP token
  • Copy the exercise files to your home directory
  • Familiarize yourself with LC's Pthreads environment
  • Write a simple "Hello World" Pthreads program
  • Successfully compile your program
  • Successfully run your program - several different ways
  • Review, compile, run and/or debug some related Pthreads programs (provided)


Mutex Variables


Mutex Variables

Creating and Destroying Mutexes



Mutex Variables

Locking and Unlocking Mutexes



Question: When more than one thread is waiting for a locked mutex, which thread will be granted the lock first after it is released?

Example: Using Mutexes

Condition Variables


Condition Variables

Creating and Destroying Condition Variables



Condition Variables

Waiting and Signaling on Condition Variables



Proper locking and unlocking of the associated mutex variable is essential when using these routines. For example:
  • Failing to lock the mutex before calling pthread_cond_wait() may cause it NOT to block.

  • Failing to unlock the mutex after calling pthread_cond_signal() may not allow a matching pthread_cond_wait() routine to complete (it will remain blocked).

Example: Using Condition Variables

Monitoring, Debugging and Performance Analysis Tools for Pthreads

Monitoring and Debugging Pthreads:

Performance Analysis Tools:

LLNL Specific Information and Recommendations

This section describes details specific to Livermore Computing's systems.



Mixing MPI with Pthreads:

Topics Not Covered

Several features of the Pthreads API are not covered in this tutorial. These are listed below. See the Pthread Library Routines Reference section for more information.

Pthread Exercise 2

Mutexes, Condition Variables and Hybrid MPI with Pthreads

  • Login to the LC workshop cluster, if you are not already logged in
  • Mutexes: review and run the provided example codes
  • Condition variables: review and run the provided example codes
  • Hybrid MPI with Pthreads: review and run the provided example codes


This completes the tutorial.

Evaluation Form       Please complete the online evaluation form - unless you are doing the exercise, in which case please complete it at the end of the exercise.

Where would you like to go now?

References and More Information

Appendix A: Pthread Library Routines Reference