Level 0 · Module 2

Threads

Learn how multiple execution paths can work inside one process.

Difficulty
beginner
Time
9 min read
Prerequisites
Processes
Reading progress0% read

Not completed

By the end, you can

  • Define a thread as an execution path within a process.
  • Compare resources shared by threads with per-thread execution state.
  • Explain one benefit and one risk of multiple threads.

Summary

A thread is a sequence of instructions being executed inside a process. Threads in one process share memory and resources, but each has its own call stack and current instruction position.

Introduction

A program may need to respond to input while doing other work. Multiple threads let the OS schedule separate execution paths within the same process.

Explanation

Every process starts with at least one thread. Additional threads can handle different tasks and may run at overlapping times—or truly at the same time on different CPU cores. Sharing the process’s memory makes communication convenient, but unsynchronized updates can interfere with each other. Coordination tools such as locks are explored later in the course.

Real-world example

Keeping an editor responsive

One thread handles typing and button clicks while another saves a large document. Both belong to the same editor process and can access its document data, so their updates must be coordinated.

Shared process, separate paths

thread-layout.txttext
EDITOR PROCESS: shared document and open file
├─ thread A: UI stack, current instruction
└─ thread B: save stack, current instruction

Code example

unsafe-update.txttext
shared progress = 0
thread A reads 0
thread B reads 0
thread A writes 1
thread B writes 1   # one update was lost

Common mistakes

  • Treating a thread as a completely isolated process.
  • Assuming concurrent work must execute simultaneously.
  • Forgetting that shared writable data needs coordination.

Quiz

Knowledge check

Which statement best describes two threads in one process?

Try it

Split the work

Propose two threads for a chat app. Name the work each performs and one piece of shared state that needs careful coordination.

Key takeaways

  • Threads are schedulable execution paths inside a process.
  • Threads share process resources but retain per-thread execution state.
  • Shared state enables cooperation and creates coordination risks.
View module overview