Course contentsShow
← Computer Science
Lesson 1278 of 2,87230. Process SynchronizationFree lesson

Atomicity and Indivisible Operations

Understanding atomic operations as instructions that complete without interruption, preventing race conditions.

Atomicity and Indivisible Operations

You'll learn what makes an operation "atomic" and why this matters for preventing race conditions.

When we talk about atomic operations, we mean instructions that execute completely, from start to finish, without interruption. The word "atomic" comes from the Greek atomos, meaning "indivisible"—just like you can't split an atom (in this metaphor!), you can't split an atomic operation.

Think of an atomic operation like swallowing a pill: you either swallow it completely or you don't. There's no half-swallowed state visible to the outside world. In contrast, a non-atomic operation is like eating a sandwich—someone might observe you halfway through, with the sandwich partially consumed.

Why Atomicity Matters

Remember our race condition problem with shared counters? When multiple threads execute count = count + 1, that seemingly simple statement actually breaks down into three steps:

  1. Read the current value of count
  2. Add 1 to that value
  3. Write the result back to count

Between any of these steps, another thread could interrupt and perform its own read-modify-write sequence. This interleaving causes lost updates.

The Solution

An indivisible operation guarantees that all its steps happen as one uninterruptible unit. If we had an atomic increment operation, the read-add-write sequence would complete entirely before any other thread could access that memory location. No interleaving, no race condition, no problem.

Hardware typically provides special atomic instructions (like compare-and-swap or test-and-set) that execute without interruption, even on multiprocessor systems. These form the building blocks for implementing mutual exclusion and critical section solutions.

Key Takeaway: Atomic operations are indivisible instructions that complete without interruption, providing the foundation for preventing race conditions in concurrent programs.