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. Mutex Variables
    1. Mutex Variables Overview
    2. Creating and Destroying Mutexes
    3. Locking and Unlocking Mutexes
  7. Condition Variables
    1. Condition Variables Overview
    2. Creating and Destroying Condition Variables
    3. Waiting and Signaling on Condition Variables
  8. LLNL Specific Information and Recommendations
  9. Topics Not Covered
  10. Pthread Library Routines Reference
  11. References and More Information
  12. Exercise


Abstract


In shared memory multiprocessor architectures, such as SMPs, 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: Ideal for those who are new to parallel programming with threads. A basic understanding of parallel programming in C is assumed. 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?



Pthreads Overview

Designing Threaded Programs

Parallel Programming:

Shared Memory Model:

Thread-safeness:

threadunsafe


The Pthreads API


Compiling Threaded Programs



Thread Management

Creating and Terminating Threads

Routines:

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

Routines:

Joining:

Joinable or Not?

Detaching:

Recommendations:


Example: Pthread Joining



Thread Management

Stack Management

Routines:

Preventing Stack Problems:

Some Practical Examples at LC:


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:

Monitoring and Debugging Threads:



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