Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

A thread (short for a thread of execution) is a single stream of instructions. A process is an instance of a currently running program. A process is composed of a single thread’s execution, or multiple threads, which execute concurrently.

Threads are an easy way to describe/think about parallelism, but their implementation is quite complicated and out of the scope of this course. Nevertheless, we describe some details that will help you understand the thread model of execution.

2Thread model of execution

2.1Thread state

Each thread maintains state as shown in Figure 1:

"TODO"

Figure 1:Single-threaded process vs. multi-threaded process.

2.2Fork-Join Model

We assume that multi-threaded processes run using the fork-join model in Figure 2:

"TODO"

Figure 2:Fork-join model over time with multiple parallel tasks off the main thread. Top: Parallel Task I is composed of concurrent threads A, B, C; Task II is composed of A, B, C, and D; Task III is composed of A, B. Bottom: Main Thread forks into the three threads for Parallel Task I, then joins, then forks into the four threads for Parallel Task II, then joins, then forks into the two threads for Parallel Task III, then joins and finishes execution.

3A Warning about Threads

From UC Berkeley Professor Emeritus Edward Lee:

Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism. Threads, as a model of computation, are wildly nondeterministic, and the job of the programmer becomes one of pruning that nondeterminism.

“The Problem with Threads.” Edward Lee[1]

As we will see over the next few sections, thread-level programming is hard.

4Executing Threads on Hardware

We are so sorry,[2] but we will introduce one more set of terms to describe how threading works in hardware:

A special program called the Operating System “multiplexes”[3] multiple software threads onto the available hardware threads. With the OS’s help, a single-core CPU can “concurrently” execute many threads by time-sharing the processor between the threads, as shown in Figure 3.

"TODO"

Figure 3:Process over time when executing multiple threads on a single-core CPU.

5The OS: Thread Context Switch

On most modern computers, the number of active threads is much larger than the number of available cores, so most (software) threads are idle at any given time. The Operating System, or OS, is responsible for (among other tasks) managing which threads get run on which CPU via a process called context switching.

The OS performs a thread context switch for two main reasons:

To switch to a different thread in the process, the OS does the following:

  1. Removes the old software thread from the hardware thread by interrupting its execution. Save the old software thread’s state, e.g., register values (including PC value) and stack pointer to memory. Because threads in the same process share memory, we keep any memory tables.[4]

  2. Start executing a different software thread. Load its state into the hardware thread’s registers (including the thread’s PC value). Then, run the hardware thread by reading the value of the PC (which is the address of the next instruction of the newly active thread).

The OS also performs context switches to multiplex different processes; for now, we won’t discuss this. Most of the details of the operating system are out of scope, but we hope a future version of these course note will go into the operating system in more detail. For those interested, please check out CS 61C Spring 2025.

6Hardware Multithreading

Up until now we have maintained that one core has one hardware therad running on it. Some architectures can support hardware multithreading—when we run multiple hardware threads* on the same core.

Intel chips use hardware multithreading[5], whereas many modern Apple chips do not. The below lscpu command run on our course hive machines tells us that we have six physical cores and two threads per core for a total of 12 logical CPUs.

$ lscpu
CPU(s):                   12
  On-line CPU(s) list:    0-11
Vendor ID:                GenuineIntel
  Model name:             Intel(R) Core(TM) 
                          i7-8700T CPU @ 2.40GHz
    CPU family:           6
    Model:                158
    Thread(s) per core:   2
    Core(s) per socket:   6
    Socket(s):            1

Briefly—the hardware multithreading model is in Figure 4. Here, each core can run multiple threads at the same time. The two hardware threads share resources like the cache, the ALU unit, etc., but have separate state (PC, registers, etc.) This design leverages “Moore’s Law” because transistors are aplenty.

"TODO"

Figure 4:Hardware multithreading: multiple threads active in the same processor.

Hardware multithreading reduces the overhead of a context switch. When the active hardware thread encounters a cache miss, the other hardware thread can be swapped in quickly and run until the data for the original hardware thread available.

Footnotes
  1. Edward A. Lee. “The Problem with Threads.” Technical Report No. UCB/EECS-2006-1. January 2006. See also: Computer 39, 5 (May 2006), 33–42. DOI

  2. From an earlier section: “Because of the abrupt shift in processor design towards parallelism, there are a LOT of closely related terms when it comes to paralellism.”

  3. To multiplex means to combine multiple channels into one shared channel (e.g., MUX). In this case, think of multiplexing as a way that multiple software threads can “time share” the same hardware thread.

  4. We describe memory tables in our section on virtual memory.

  5. Intel uses yet another term to describe hardware multithreading: hyperthreading. Woo terminology!!

References
  1. Lee, E. A. (2006). The Problem with Threads. Computer, 39(5), 33–42. 10.1109/mc.2006.180