Threads

The Thread Concept

What is a Thread?

Thread: Basic unit of CPU utilization.

Purpose — Threads let us avoid incurring the overhead of creating and managing separate PCBs.

Structure

A thread is much lighter than a PCB and contains:

It shares the following with other threads in the same process.

Example — A word processor may have a thread for getting keystrokes from the user, another for spellcheck, and another for graphics.

Example: Single v. Multithreaded Process
Single-Threaded ProcessMulti-Threaded ProcessThread 1Thread 2CodeDataHeapStackRegistersPCCodeDataHeapStackRegistersPCStackRegistersPC

Key Difference: In the multithreaded process, the Code, Data, and Heap are shared, while each thread gets its own private Stack, Registers, and Program Counter (PC).

Motivation and Benefits

Motivation

Aside — Today, kernels and modern applications are generally multithreaded.

Benefits

Example: Multithreaded Server Architecture
  1. A server listens for requests.
  2. When it gets a request, it makes a thread to service it.
  3. The server can quickly resume listening for requests.

Concurrency and Parallelism

Concurrency: Supports more than one task making progress.

Parallelism: System can do more than one task simultaneously.

Types of Parallelism:

Data Parallelism: Splits data across multiple cores, doing same operation on each.

Example: Data Parallelism

You want to sum an array on a dual-core system.

You could have one core sum-up the first half, and the other core do the latter half.

Task Parallelism: Distribute threads across cores. Each thread can do unique operations on same or different data.

Challenges of Multicore Programming

Multicore or multiprocessor systems put pressure on programmers, such as:

Amdal’s Law

Identifies performance gains from adding additional cores to an application that has serial and parallel components.

\text{speedup} \le \frac{1}{ S + \frac{1-S}{N} } \\~\\ \small \textit{where $S$ is serial portion; $N$ is processing cores}

The serial portion has a disproportionate effect on performance.

Thread Models

User Threads v. Kernel Threads

User: Supported above the kernel, managed without kernel support.

Kernel: Managed directly by the kernel.

Aside — All modern operating systems support multithreading.

Multithreading Models

1. Many-to-One Model

Examples — Solaris Green Threads Library

2. One-to-One Model

Examples — Windows, Linux, and many programming languages.

3. Many-to-Many Model

Examples — Windows with ThreadFiber package

4. Two-Level Model

Examples — Tru64 UNIX

Thread Library

Provides programmer with API for creating and managing threads

Two Primary Implementation:

  1. Entirely in userspace
  2. Supported by kernel

Examples:

Java Threads

Managed by the JVM

You can create a thread by implementing the Runnable interface.

class Mytask extends SomeOtherClass implements Runnable {
    // ...
}

Note — Don’t extend the Thread class, you’ll lock-out extending anything else!