1. Home
  2. Explore
  3. Computer Science
  4. Threads Lecture Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Threads Lecture Overview

Threads in Operating Systems

Motivation and Benefits

Traditional single-threaded processes execute one thing at a time. In modern systems, many applications benefit from doing multiple things simultaneously:

  • Games: need to handle keyboard input (shoot/move), update the display, and run physics — all concurrently

  • Video rendering: the same algorithm can be applied to different chunks of data in parallel to speed up processing

  • Network traffic and I/O: background operations (e.g., spell checking in a word processor) run without blocking the main interface

Using threads rather than separate processes is less resource-heavy. A thread is a lightweight unit of execution that shares the same code, data, and file resources as its parent process. This avoids the overhead of creating a new process (as seen in earlier topics on process management).

Key Benefits

  • Responsiveness: a multithreaded application remains interactive even when one thread is blocked (e.g., a spreadsheet calculating in the background)

  • Resource sharing: threads within the same process automatically share memory and data — no need for message passing (which adds overhead)

  • Economy: creating and switching threads is much cheaper than doing so for processes (less CPU time, less memory)

  • Scalability: as hardware gains more cores or processors, multithreaded processes can take advantage of them without being rewritten

Thread – a unit of execution within a process that can run concurrently (or in parallel) with other threads of the same process. It is not a process; it is a part of a process.

Parallelism vs Concurrency

  • Concurrency: multiple tasks make progress over time, but only one executes at any instant (typical of time‑shared single‑core systems — discussed in previous weeks)

  • Parallelism: multiple tasks actually run at the same time, requiring multiple cores or processors

Two common approaches to parallelism:

  1. Data parallelism: split the data into chunks and distribute each chunk to a thread that runs the same operation (e.g., rendering an image by dividing it into sections)

  2. Task parallelism: threads perform different tasks simultaneously (e.g., one thread handles the display, another the spell check)

Both require hardware support — CPUs with multiple cores or multiple processors — and the operating system must provide the services to create and manage threads.

Amdahl's Law (Speedup Limits)

The speedup gained by adding more cores is not linear. A program consists of:

  • A serial portion (must be executed one step after another — e.g., loading a file)

  • A parallel portion (can be split across threads)

The serial fraction acts as a bottleneck. Even with infinite cores, the speedup is limited by the serial part.

Example: If an application is 25% serial and 75% parallel, adding a second core only yields a 1.6× speedup, not 2×. The serial portion drags overall performance.

Formula (conceptual, not required to memorize):

Speedup=1(1−P)+PN\text{Speedup} = \frac{1}{(1-P) + \frac{P}{N}}Speedup=(1−P)+NP​1​

where P is the parallel fraction and N is the number of cores.


Thread Models

User‑level threads are created by the programmer via thread libraries. These must be mapped to kernel‑level threads that the operating system schedules. Different mapping models exist:

Many‑to‑One

  • Many user threads map to a single kernel thread

  • No real parallelism (only concurrency)

  • A blocking call in any user thread blocks all of them

One‑to‑One (most common)

  • Each user thread maps to one kernel thread

  • True parallelism is possible

  • Used by Windows and Linux

  • Potential limitation: the OS may restrict the number of kernel threads a process can create

Many‑to‑Many

  • Many user threads are multiplexed onto a smaller or equal number of kernel threads

  • More flexible, but more complex to manage

  • The OS decides how many kernel threads are needed

Two‑Level Model

  • Hybrid: many‑to‑many with the option to bind specific user threads directly to a kernel thread (one‑to‑one) for special handling

  • Gives programmers fine‑grained control


Thread Libraries

Programmers use libraries to create and manage threads. Key libraries include:

  • Pthreads (POSIX threads): standard for Unix‑based systems (Linux, macOS). Defines how threads should work, but implementations may differ (e.g., left vs. right steering wheel analogy)

  • Windows API: provides CreateThread and related calls; uses a one‑to‑one model

  • Java threads: built into the language; maps to underlying OS threads

The kernel must support the mapping. When a user creates a thread, the OS creates a corresponding kernel thread (or uses an existing one) and schedules it.


Windows Thread Implementation

Each thread in Windows has:

  • A thread ID (unique identifier)

  • A set of registers (CPU state)

  • A stack (for local variables and call history)

  • A thread context – the full state of the thread at any moment

Internally, the system uses:

  1. Executive thread block: tracks the thread’s state, which process started it, and other OS‑managed information

  2. Kernel thread block: contains scheduling info, synchronization data, stack pointers, etc.

  3. User‑space thread: the actual code the user runs

This structure lives partly in kernel space and points to user‑space data.


Linux Threads (Tasks)

Linux does not distinguish sharply between processes and threads; it calls both tasks. A new thread is created via the clone() system call.

clone() allows fine‑grained control over what is shared with the parent:

  • Code (instructions)

  • Data (memory)

  • Files (open file descriptors)

  • Signal handlers

  • Network connections
    …or any combination.

Unlike fork() (which duplicates most resources), clone() can share them, making thread creation very efficient.


System Calls (Quick Review)

A system call is the mechanism by which a user process requests a service from the operating system. Examples:

  • File operations (open, read, write)

  • Network communication

  • Display updates

  • Mouse/keyboard input

The OS handles the low‑level details and returns the result. Thread creation itself is often performed via a system call (e.g., CreateThread on Windows, clone on Linux).


In summary: Threads enable a single process to perform multiple tasks concurrently or in parallel, sharing resources to minimize overhead. The choice of thread model and library depends on the operating system and hardware. While threads improve performance and responsiveness, they introduce programming challenges (race conditions, deadlocks, etc.) that require careful handling — but those details are beyond this introductory scope.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent