CSC 357 Lecture Notes Week 10
Introduction to Threads



  1. Reading -- Stevens Chapter 11.

  2. An introduction to POSIX threads, aka pthreads.
    1. POSIX threads allow a single process to have multiple threads of control.
    2. They are sometimes referred to as "light-weight processes", since only a single UNIX "heavy-weight process" need exist for a multi-thread program.
    3. There are many similarities between threads and processes, as illustrated in the following table from Stevens Page 367:
      Process Primitive Thread Description
      fork pthread_create create a new flow of control
      exit pthread_exit exit from an existing flow of control
      waitpid pthread_join get exit status from flow of control
      atexit pthread_cleanup_push register function to be called at exit from flow of control
      getpid pthread_self get ID for flow of control
      abort pthread_cancel request abnormal termination for flow of control
    4. See the example p-test program in the Lab 8 writeup.

  3. Thread synchronization.
    1. Since threads run in the same process, they share the same process memory, in particular global variables and signal handlers.
    2. To avoid race conditions on memory access, the pthreads function set includes support for the following synchronization primitives:
      1. mutexes -- a lock that provides mutually exclusive access to a shared resource
      2. reader-writer locks -- higher-level form of mutex that allows separation of readers versus writers of a shared resource; basically, the resource can have multiple concurrent readers, but only one writer at a time
      3. condition variables -- provide a form of thread rendezvous when specified condition has been met; a signaling function is used to indicate that a condition has been satisfied

  4. Well-known synchronization issues.
    1. deadlock -- two threads each have a mutex on a resource that the other process wants
    2. starvation -- when the scheduling of threads systematically allows only certain threads to run, due to a lack of or unfair thread prioritization

index | lectures | labs | programs | handouts | solutions | examples | documentation | bin