---
title: "Threads and Shared Memory"
description: "Threads inside one process share an address space and everything in it, which makes them cheap to switch between and dangerous to reason about. The gain is a program that keeps working while one part "
canonical: https://lightmysky.com/learn/computing/threads-and-shared-memory-mt_N4EGNbN6Xh
source: https://lightmysky.com/learn/computing/threads-and-shared-memory-mt_N4EGNbN6Xh.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Threads and Shared Memory

Threads inside one process share an address space and everything in it, which makes them cheap to switch between and dangerous to reason about. The gain is a program that keeps working while one part waits; the cost is that any two threads can touch the same variable.

Subject: Computing · Area: Computer Systems · Ages 20 to 21
Page: https://lightmysky.com/learn/computing/threads-and-shared-memory-mt_N4EGNbN6Xh

## Ready when they can

- Say what threads in one process share and what each keeps to itself
- Explain why creating a thread is cheaper than creating a process
- Identify a variable in a small program that two threads could reach at once

## Lesson: Threads share one memory, for better and worse

An address space is the set of addresses a program uses to reach its instructions and data. Every process gets its own, kept private by address translation, so two programs cannot read each other's memory. Threads bend this rule on purpose: all threads inside one process live in the same address space, reading and writing the same variables with no barrier between them.

**Example.** Picture a counter called total that two threads both raise by one. Either thread can reach total at any moment, since it sits in their shared space. If both read it at the same time and both write back, one raise gets lost and the threads have stepped on each other. Each thread keeps its own CPU state, its program counter, stack pointer and registers, but the variables are shared.

That sharing is why creating a thread is cheaper than creating a process. A new process needs a fresh address space with fresh translation, while a new thread reuses the one already there. Switching between threads of one process is cheap for the same reason: the shared space stays put, so there is far less to swap.

**Tip.** The gain is a program that keeps working while one part waits, since another thread can run meanwhile. The cost is that any two threads can touch the same variable, so guard shared data and let only one thread change it at a time. Keep the sharing for speed, and remove the stepping for safety.

**Recap.** Threads share one address space, which makes them cheap and lets them collide, so guard the data they share.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Virtual Memory and Address Translation](https://lightmysky.com/learn/computing/virtual-memory-and-address-translation-mt_B-PiAyderj)

## Opens up

- [Race Conditions and Mutual Exclusion](https://lightmysky.com/learn/computing/race-conditions-and-mutual-exclusion-mt_hygT74jJDE)
