POSIX Threads Programming


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. Compiling Threaded Programs
  4. Thread Management
    1. Creating and Terminating Threads
    2. Passing Arguments to Threads
    3. Joining and Detaching Threads
    4. Stack Management
    5. Miscellaneous Routines
  5. Mutex Variables
    1. Mutex Variables Overview
    2. Creating and Destroying Mutexes
    3. Locking and Unlocking Mutexes
  6. Condition Variables
    1. Condition Variables Overview
    2. Creating and Destroying Condition Variables
    3. Waiting and Signaling on Condition Variables
  7. LLNL Specific Information and Recommendations
  8. Topics Not Covered
  9. Pthread Library Routines Reference
  10. References and More Information
  11. Exercise


Pthreads Overview

What is a Thread?

Process-Thread Relationship:



Pthreads Overview

What are Pthreads?



Pthreads Overview

Why Pthreads?



Pthreads Overview

Designing Threaded Programs



The Pthreads API


Compiling Threaded Programs



Thread Management

Creating and Terminating Threads

Routines:

Creating Threads:

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

Thread Attributes:

Terminating Threads:


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

Routines:

Joining:

Joinable or Not?

Detaching:

Recommendations:


Example: Pthread Joining



Thread Management

Stack Management

Routines:

Preventing Stack Problems:


Example: Stack Management



Thread Management

Miscellaneous Routines



Mutex Variables

Overview



Mutex Variables

Creating and Destroying Mutexes

Routines:

Usage:



Mutex Variables

Locking and Unlocking Mutexes

Routines:

Usage:

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

Overview



Condition Variables

Creating and Destroying Condition Variables

Routines:

Usage:



Condition Variables

Waiting and Signaling on Condition Variables

Routines:

Usage:

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



LLNL Specific Information and Recommendations


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

Implementations:

Compiling:

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 Library Routines Reference


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