Concurrency

What Is Concurrency in Operating Systems?

In operating systems, concurrency means several tasks make progress over the same period, taking turns on one core or running at once on many cores.

On this page
  1. Concurrency and parallelism are not the same
  2. How one core shares its time
  3. Processes and threads
  4. What goes wrong
  5. A small example in C++
  6. Where C++ fits
  7. In short

In an operating system, concurrency means several tasks are in progress during the same period. The tasks may run at the same instant on different CPU cores, or the system may switch between them so quickly that they seem to run together on one core. Either way, the operating system decides which task gets the processor, and it keeps tasks from damaging each other’s data.

Concurrency and parallelism are not the same

Concurrency describes how a system is organized to handle many tasks. Parallelism describes tasks that are actually running at the same instant. A computer with one processor core can be concurrent without being parallel. It switches between a browser, a music player and a text editor, and none of them runs at the exact same moment as another.

A system with several cores can run some of those tasks in parallel, which can make work finish sooner. The two ideas often show up together, but they are not the same thing.

How one core shares its time

A CPU core runs one stream of instructions at a time. To give the impression of many tasks, the operating system uses time slicing. It lets a task run for a short slice of time, saves its state, and starts another task. The part of the OS that makes these decisions is the scheduler.

Switching from one task to another is called a context switch. The OS saves the registers and other state of the old task, then restores the state of the new one. A context switch takes time, so systems try not to switch more often than they need to.

Processes and threads

Operating systems usually run programs as processes. Each process has its own address space, so one process cannot read another’s memory by accident. A process can contain one or more threads. Threads in the same process share memory, including global variables and heap-allocated objects. Each thread has its own stack and its own place in the code.

Creating a thread is generally cheaper than creating a process, and threads can pass data to each other through shared memory without copying it. That sharing is what makes threads useful, and it is also the source of most concurrency bugs.

What goes wrong

Two problems make concurrency hard.

A race condition happens when the result depends on the timing of tasks. Two tasks that update the same file or variable can finish in an order that gives a wrong answer. The bug may appear only sometimes, which makes it hard to find.

A deadlock happens when tasks wait on each other, and none of them can continue. Task A holds a resource that task B needs, while task B holds a resource that task A needs.

Operating systems and programs both use locks, semaphores and similar tools to coordinate access. Good design keeps shared work small and easy to follow.

A small example in C++

The following program starts two threads. Each one prints letters, and the order of the output is not fixed:

#include <iostream>
#include <thread>

void print_letters(char letter) {
    for (int i = 0; i < 3; ++i) {
        std::cout << letter;
    }
}

int main() {
    std::thread a(print_letters, 'A');
    std::thread b(print_letters, 'B');
    a.join();
    b.join();
    std::cout << '\n';
}

The output could be AAABBB, BBBAAA, or a mix of letters from both threads, and it can change between runs. The scheduler decides when each thread runs, so the program should not depend on any particular order.

Where C++ fits

The operating system provides threads, and C++ exposes them through std::thread, which arrived in C++11. The same ideas apply in code: threads need a way to coordinate when they share data. The guide to concurrency in c covers mutexes, futures and atomics in more depth.

In short

Concurrency in an operating system is the management of many tasks in progress at once. Time slicing and multiple cores make it possible, and locks and careful design keep shared data correct.

The complete guide to concurrency

Concurrency in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

List vs Array in C++

Compare C++ arrays, std::array, std::vector and std::list by memory layout, element access and insertion cost, with code and a simple rule for choosing.

List vs Array 5 min read

std::optional in C++

std::optional in C++17 holds either a value or nothing. Learn how to check it, read it safely with value_or and return it from a function, with examples.

std::optional 4 min read