Skip to main content

Threads and Concurrency

Thread Concepts

0:00
LearnStep 1/2

What are Threads?

Threads: Lightweight Processes

A thread is the smallest unit of CPU execution within a process.

Process vs Thread

AspectProcessThread
MemorySeparate address spaceShared address space
CommunicationIPC (pipes, sockets)Shared memory (direct)
Creation timeSlow (heavy)Fast (lightweight)
Context switchExpensiveCheaper
IsolationStrongWeak (crash affects all)

Thread Components

Each thread has its own:

  • Thread ID
  • Program counter
  • Register set
  • Stack

Threads share:

  • Code section
  • Data section (global variables)
  • Heap
  • Open files and signals

Process Memory Layout with Threads:

text