Table of Contents

  1. Pthreads Overview
    1. What is a Thread?
    2. What are Pthreads?
    3. Why Pthreads?
    4. Designing Threaded Programs
  2. The Pthreads API
  3. Thread Management
    1. Creating Threads
    2. Terminating Thread Execution
    3. Example: Pthread Creation and Termination
    4. Passing Arguments to Threads
    5. Thread Identifiers
    6. Joining Threads
    7. Detaching / Joining Threads
    8. Example: Joining Threads
  4. Mutex Variables
    1. Mutex Variables Overview
    2. Creating / Destroying Mutexes
    3. Locking / Unlocking Mutexes
    4. Example: Using Mutexes
  5. Condition Variables
    1. Condition Variables Overview
    2. Creating/Destroying Condition Variables
    3. Waiting / Signalling on Condition Variables
    4. Example: Using Condition Variables
  6. LLNL Specific Information and Recommendations
  7. Pthread Library Routines Reference
  8. References and More Information
  9. Exercise
  10. Workshop Home


Pthreads Overview

What is a Thread?




Pthreads Overview

What are Pthreads?




Pthreads Overview

Why Pthreads?




Pthreads Overview

Designing Threaded Programs




The Pthreads API




Thread Management

Creating Threads


Question: After a thread has been created, how do you know when it will be scheduled to run by the operating system...especially on an SMP machine?


Thread Management

Terminating Thread Execution




Thread Management

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

Thread Identifiers




Thread Management

Joining Threads




Thread Management

Detaching / Joining Threads




Thread Management

Example: Pthread Joining




Mutex Variables

Overview




Mutex Variables

Creating / Destroying Mutexes




Mutex Variables

Locking / 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?


Mutex Variables

Example: Using Mutexes




Condition Variables

Overview




Condition Variables

Creating / Destroying Condition Variables




Condition Variables

Waiting / Signalling 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).


Condition Variables

Example: Using Condition Variables




LLNL Specific Information and Recommendations


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

Implementations:

Compiling:

Mixing MPI with Pthreads:



Pthread Library Routines Reference




References and More Information