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


Abstract


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 one of the eight tutorials in the 4+ day "Using LLNL's Supercomputers" workshop. It is deal for those who are new to parallel programming with threads. 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-safeness:

Thread Limits:



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



Pthread Exercise 1

Getting Started and Thread Management Routines

Overview:
  • 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)

GO TO THE EXERCISE HERE



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



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.

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 Exercise 2

Mutexes, Condition Variables and Hybrid MPI with Pthreads

Overview:
  • 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

GO TO THE EXERCISE HERE






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