Lecture 39

Threads and concurrency

MCS 260 Fall 2021
David Dumas


  • Project 4 autograder open
  • Worksheet 14 and solutions available
  • Homework 14 coming tomorrow, due Tue Nov 30
  • No lab tomorrow - replaced by Johnny's office hours
  • Kylash has extra office hours Wed 12-2
  • No synchronous lecture Wed; will post video instead
  • Concurrency — The ability to have several independent operations underway at the same time
  • Parallelism — The ability to have several independent operations running at the same time

Thus parallelism is one way to achieve concurrency. Pausing and switching is another way.

E.g. a single person has very limited parallelism, but can often handle extensive concurrency.


A process is the notion of one instance of a running program, consisting of code and data loaded into memory, ready for the CPU to execute.

A process can't (normally) access the memory of other processes.

Process management and scheduling is an important OS function.


A thread is a division within a process. Each thread executes concurrently with the others.

Each process has at least one thread.

E.g. One thread handles the GUI, another handles communication with APIs. The GUI remains responsive even if an API call is slow.

Key: Threads of a single process share memory space, i.e. code and data.

CPUs and cores

A modern CPU typically contains multiple cores. For most purposes these behave as separate CPUs running in parallel.

Thus most CPUs run several processes or threads in parallel.

Single-threaded process

Sequential execution

Note: The animation above won't play in PDF slides.

Multi-threaded process (theory)

Parallel execution

Note: The animation above won't play in PDF slides.

Multi-threaded process (Python)

Concurrent execution

Note: The animation above won't play in PDF slides.

Big limitation: Only one thread of a Python program can be executing Python code at any given time.

Still, using threads for concurrency can be very useful, especially in GUI programs.

Threads of a single program often need to communicate; we'll talk about this a bit later.

Basic thread example

The threading module allows a Python program to create new threads.

Let's make a program with two threads that each print some data to the terminal.

To make a thread, subclass threading.Thread and put code for the thread in method .run(). Instantiate and call .start() to launch the thread.


The program exits when its last thread is done.

We have no direct control over order of execution in different threads.

Waiting and daemons

A threading.Thread object has a method .join() which tells the calling thread to wait until the other thread is finished.

Alternatively, a daemon thread is one that is automatically killed when the main thread exits. Create one by passing daemon=True to the threading.Thread constructor.

Worker pattern

Often, the main thread spawns some worker threads to handle slow tasks in the background.

The main thread sends jobs to the workers, and may wait for results to be returned.

This is also called the producer-consumer pattern.

Thread communication

If multiple threads access the same object, with at least one of them writing, you're asking for trouble.

Instead of directly accessing a shared object, you should use a data structure designed for communication between threads.

queue.Queue from the module queue does this. It has a .put(val) method to submit an object to the queue, and a .get() method to retrieve an object.


  • Python module documentation:
  • Section 6.6 of the optional course text by Brookshear & Brylow discusses threading and concurrency
  • A tutorial on concurrency by Jim Anderson (from RealPython)

Revision history

  • 2021-11-22 Initial publication