Overview

In previous readings, we were introduced to the concept of concurrency. In this reading we will look at two ways to achieve concurrency in our programs: processes and threads. For the latter, we’ll be introduced to several functions in the Pthreads library that will allow us to create and control threads.

Required Reading

Sections 12.1, 12.3 (excluding 12.3.8), and 12.4 (pp. 971-976, 985-995) from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Summarize how concurrency can be achieved by using multiple processes, e.g. in a server that must handle multiple concurrent requests.
  2. List the pros and cons of using processes for concurrency.
  3. Identify those parts of the process context that are shared amongst threads and those that are part of the thread context.
  4. Given a function, determine whether it may be used (without modification) as a thread routine for a pthread.
  5. Write C code that creates a new pthread that runs a specified thread routine, then joins with that new thread.
  6. Describe the four ways in which a thread may be terminated.
  7. Compare and contrast the wait and pthread_join functions.
  8. Explain why it can be advantageous for a thread to be detached rather than joinable.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Compare and contrast parallelism and concurrency.
  2. Compare and contrast threads and processes.
  3. Given a program that uses pthreads, identify the data that will be shared amongst threads and which threads it will be shared amongst.
  4. Use common pthread idioms to create a thread function that uses a non-pointer parameter, takes in multiple parameters, and/or returns multiple values.
  5. Write and trace code that uses the pthreads library that uses only pthread_join for synchronization.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. In your own words, explain why processes having isolated address spaces can be both helpful and detrimental to achieving concurrency.

  2. List the state that is private to a thread.

  3. For each of the following functions, list whether it may be used as a thread routine for a pthread, without making any modifications. Briefly explain your answers. If it is not possible to tell based on the given code, explain why.

  4. Write a C program that creates two pthreads. The first thread should run a routine that prints out the message “Pass the Tofurky!”, while the other should print out a contents of a global variable named “x”. (You should declare x in your program and set it to a value in main before creating the threads.) Your main thread should join with both of the threads.

  5. Briefly describe the four ways in which a thread may be terminated.