SyntaxPilot
Courses/C++/Lesson 34
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

C++ · Lesson 34 of 40

Multithreading

queued

These are the notes for C++ episode 34. Every program so far did one thing at a time. This episode makes a program do two things at once with std::thread, shows the crash you get without join, then lets two threads share a counter and quietly lose tickets (a race condition), and builds the fixes: mutex with lock_guard, atomic, the rule that avoids deadlock, and async with future to get results back. Every example below was compiled with g++ -O2 -std=c++17 main.cpp -o main (g++ 13.3, the same command as the SyntaxPilot compiler; with glibc 2.34 or newer no -pthread flag is needed, on older systems add -pthread) and run; outputs are copied from those runs. Timings and race results change from run to run.

The problem: one cook, two stoves

A chai stall order: chai (1 second) and a samosa (1 second). this_thread::sleep_for stands in for the cooking; steady_clock from <chrono> measures the time:

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

void makeChai() {
    this_thread::sleep_for(seconds(1));    // boil for 1 second
    cout << "chai ready" << endl;
}
void frySamosa() {
    this_thread::sleep_for(seconds(1));    // fry for 1 second
    cout << "samosa ready" << endl;
}

int main() {
    auto start = steady_clock::now();
    makeChai();
    frySamosa();
    auto ms = duration_cast<milliseconds>(steady_clock::now() - start).count();
    cout << "Order done in " << ms << " ms" << endl;
    return 0;
}

Output:

chai ready
samosa ready
Order done in 2003 ms

Why 2 seconds: a program normally runs on one thread, one path of steps, one after another. The samosa can't start until the chai is done, even though the computer has several CPU cores (like several stoves).

Step 1: a second cook, std::thread

In the kitchen In C++
Hire a second cook, who starts frying straight away thread cook2(frySamosa); starts a new thread running frySamosa
Two stoves at once two cores really run both threads at the same moment
Wait for both dishes before serving cook.join(); waits until the thread has finished
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

void makeChai() {
    this_thread::sleep_for(seconds(1));    // boil for 1 second
    cout << "chai ready" << endl;
}
void frySamosa() {
    this_thread::sleep_for(seconds(1));    // fry for 1 second
    cout << "samosa ready" << endl;
}

int main() {
    auto start = steady_clock::now();
    thread cook1(makeChai);                // start a second worker
    thread cook2(frySamosa);
    cook1.join();                          // wait for it to finish
    cook2.join();
    auto ms = duration_cast<milliseconds>(steady_clock::now() - start).count();
    cout << "Order done in " << ms << " ms" << endl;
    return 0;
}

Output:

samosa ready
chai ready
Order done in 1001 ms

Half the time. Notes:

  • Pass the function without brackets: thread t(makeChai); hands over the function; makeChai() would call it right away.
  • The order of chai ready and samosa ready can change between runs: threads run independently.
  • Printing to cout from several threads at once can mix their text together (see exercise 4).

Step 2: always join

#include <iostream>
#include <thread>
using namespace std;

void makeChai() {
    cout << "chai ready" << endl;
}

int main() {
    thread cook(makeChai);
    cout << "order taken" << endl;
    return 0;                          // forgot cook.join()
}

Output:

order taken
terminate called without an active exception
(the program is aborted)

If a std::thread object is destroyed while it is still joinable (started and not joined), C++ calls std::terminate. Every thread must be joined (or, rarely, detach()ed) before its object goes out of scope. (C++20 adds std::jthread, which joins automatically in its destructor.)

Step 3: arguments and lambdas

#include <iostream>
#include <thread>
#include <string>
using namespace std;

void makeTea(string kind, int cups) {
    cout << cups << " x " << kind << endl;
}

int main() {
    thread cook1(makeTea, "masala chai", 3);    // function, then its arguments
    cook1.join();

    int plates = 2;
    thread cook2([plates] {                     // a lambda (episode 30)
        cout << plates << " x samosa" << endl;
    });
    cook2.join();
    return 0;
}

Output:

3 x masala chai
2 x samosa

thread t(function, arg1, arg2, ...) copies the arguments into the new thread. A lambda (episode 30) is the most common way to give a thread a small job. To pass a reference, wrap it: thread t(f, ref(x));.

Step 4: the race condition

Two ticket counters, each selling a million tickets, share one count. Each sale reads the count, prints the next seat number, and writes the new count:

#include <iostream>
#include <string>
#include <thread>
using namespace std;

int booked = 0;
void sellTickets() {
    for (int i = 0; i < 1000000; i++) {
        int seen = booked;                     // read the count
        string ticket = "SEAT-" + to_string(seen + 1);
        booked = seen + 1;                     // write the new count
    }
}

int main() {
    thread counter1(sellTickets);
    thread counter2(sellTickets);
    counter1.join();
    counter2.join();
    cout << "Tickets sold: " << booked << endl;
    return 0;
}

Output (three runs):

Tickets sold: 1137578
Tickets sold: 1230422
Tickets sold: 1000000

It should be 2,000,000, and it's wrong by a different amount each time.

Why: each sale is three steps: read, make the ticket, write.

  1. Counter 1 reads 41. Before it writes, counter 2 also reads 41.
  2. Both print SEAT-42, both write 42: two sales, the count went up by one.
  3. If a counter is paused by the operating system between its read and its write, every sale the other counter made meanwhile is wiped out when it finally writes.

This is a race condition (here a data race): two threads access the same data at the same time, at least one of them writes, and nothing orders them. Even a bare booked++ is a read, an add and a write inside the CPU, so it races too, just less often. In C++ a data race is undefined behaviour.

Step 5: one pen for the register, mutex + lock_guard

At the station In C++
One pen on a chain; only its holder may write mutex registerPen;: only one thread can hold (lock) it at a time
Take the pen, write, put it back; the other clerk waits lock_guard<mutex> lock(registerPen); locks now and unlocks at the closing brace
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
using namespace std;

int booked = 0;
mutex registerPen;                     // only one holder at a time
void sellTickets() {
    for (int i = 0; i < 1000000; i++) {
        lock_guard<mutex> lock(registerPen);   // take the pen
        int seen = booked;                     // read the count
        string ticket = "SEAT-" + to_string(seen + 1);
        booked = seen + 1;                     // write the new count
    }
}

int main() {
    thread counter1(sellTickets);
    thread counter2(sellTickets);
    counter1.join();
    counter2.join();
    cout << "Tickets sold: " << booked << endl;
    return 0;
}

Output (every run):

Tickets sold: 2000000

The part between taking and releasing the lock is the critical section. lock_guard releases the mutex in its destructor (RAII), even if an exception is thrown, so you never forget unlock(). Keep critical sections small: while one thread holds the lock, the others wait.

Step 6: atomic for a single counter

#include <iostream>
#include <string>
#include <thread>
#include <atomic>
using namespace std;

atomic<int> booked = 0;                // ++ in one unbreakable step
void sellTickets() {
    for (int i = 0; i < 1000000; i++) {
        int seat = ++booked;                   // read, add, write: one step
        string ticket = "SEAT-" + to_string(seat);
    }
}

int main() {
    thread counter1(sellTickets);
    thread counter2(sellTickets);
    counter1.join();
    counter2.join();
    cout << "Tickets sold: " << booked << endl;
    return 0;
}

Output:

Tickets sold: 2000000

++booked on an atomic<int> reads, adds and writes as one indivisible step and returns the new value, so every seat number is unique. Use atomic for a single number or flag; use a mutex when several pieces of data must change together (move money from one account to another).

Step 7: deadlock

Cook A holds the big ladle and waits for the small one; cook B holds the small ladle and waits for the big one. Neither lets go: the program hangs forever. That's a deadlock. In code: thread A locks m1 then m2, thread B locks m2 then m1.

Avoid it:

  • Lock mutexes in the same order everywhere.
  • Lock several at once with scoped_lock lock(m1, m2); (C++17), which avoids deadlock for you.
  • Don't call unknown code while holding a lock, and hold locks briefly.

Step 8: results back with async and future

At the food court In C++
Order and get a token; the kitchen starts cooking async(launch::async, f, args...) runs f on another thread
Hold the token, do other things future<T>: a value that will arrive later
Show the token and collect the food (wait if it isn't ready) f.get() waits for the thread and returns its result
#include <iostream>
#include <future>
#include <vector>
using namespace std;

long long totalSales(vector<int> bills) {
    long long sum = 0;
    for (int b : bills) sum += b;
    return sum;
}

int main() {
    vector<int> morning = {120, 250, 80, 40};
    vector<int> evening = {300, 150, 90};

    future<long long> f1 = async(launch::async, totalSales, morning);
    future<long long> f2 = async(launch::async, totalSales, evening);

    cout << "Waiting for the totals..." << endl;
    cout << "Day total: " << f1.get() + f2.get() << endl;
    return 0;
}

Output:

Waiting for the totals...
Day total: 1030

No shared variable, no mutex, no join: get() waits and brings back the return value (and rethrows any exception the function threw). Call get() only once per future. launch::async asks for a new thread; without it the library may run the function lazily, inside get().

Cheat sheet

Need Code
Run a function in parallel thread t(f, args...);
Wait for it t.join(); (always)
Protect shared data mutex m; ... lock_guard<mutex> lock(m);
A shared counter atomic<int> count = 0; count++;
Lock two mutexes safely scoped_lock lock(m1, m2);
Get a result back auto f = async(launch::async, f, x); f.get();
Pause a thread this_thread::sleep_for(milliseconds(100));
How many cores? thread::hardware_concurrency()

Common mistakes

Mistake Real output Fix
A thread that's never joined terminate called without an active exception (aborted) t.join();
Two threads writing the same variable without a lock Tickets sold: 1137578 (should be 2000000), different every run lock_guard<mutex> or atomic
Locks taken in opposite orders the program hangs forever (deadlock) same order everywhere, or scoped_lock
thread t(makeChai()); error: invalid use of void expression thread t(makeChai);
Calling get() twice on a future the second call throws std::future_error (no state) call get() once, keep the value

Mini project: split the work across 4 threads

Sum a function over 1 to 200,000,000 in one thread, then split the range into four parts with async:

#include <iostream>
#include <future>
#include <vector>
#include <chrono>
using namespace std;
using namespace std::chrono;

long long sumRange(long long from, long long to) {    // from..to, inclusive
    long long s = 0;
    for (long long i = from; i <= to; i++) s += i % 7;  // some real work per number
    return s;
}

int main() {
    const long long N = 200000000;

    auto t0 = steady_clock::now();
    long long one = sumRange(1, N);
    auto t1 = steady_clock::now();

    vector<future<long long>> parts;
    for (int k = 0; k < 4; k++)
        parts.push_back(async(launch::async, sumRange, k * N / 4 + 1, (k + 1) * N / 4));
    long long four = 0;
    for (auto& f : parts) four += f.get();
    auto t2 = steady_clock::now();

    cout << "1 thread:  " << one  << " in " << duration_cast<milliseconds>(t1 - t0).count() << " ms" << endl;
    cout << "4 threads: " << four << " in " << duration_cast<milliseconds>(t2 - t1).count() << " ms" << endl;
    return 0;
}

Output (on a busy 2-core machine; your times will differ):

1 thread:  599999998 in 334 ms
4 threads: 599999998 in 404 ms

Same answer, but not faster here: this machine had only two cores, and one was busy with another program. More threads than free cores doesn't help, and starting threads has a cost. On a free 4-core laptop the 4-thread version runs about 3-4 times faster. Always measure.

Practice

  1. Time three one-second tasks, in a row and in three threads.
  2. Fix the ticket counter with a mutex, then with atomic (steps 5 and 6).
  3. Sum 1 to 100,000,000 in 4 threads with async (the mini project).
  4. Keep two threads' printed lines from mixing, using a mutex.
Solutions

Exercise 1:

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

void task() { this_thread::sleep_for(seconds(1)); }

int main() {
    auto t0 = steady_clock::now();
    task(); task(); task();
    auto t1 = steady_clock::now();
    thread a(task), b(task), c(task);
    a.join(); b.join(); c.join();
    auto t2 = steady_clock::now();
    cout << "in a row:   " << duration_cast<milliseconds>(t1 - t0).count() << " ms" << endl;
    cout << "in threads: " << duration_cast<milliseconds>(t2 - t1).count() << " ms" << endl;
    return 0;
}

Output:

in a row:   3000 ms
in threads: 1004 ms

Exercise 4 (lock the mutex for each whole line; the two counters' lines can interleave, but never within a line):

#include <iostream>
#include <thread>
#include <mutex>
#include <string>
using namespace std;

mutex screen;

void announce(string counter) {
    for (int i = 1; i <= 3; i++) {
        lock_guard<mutex> lock(screen);      // one whole line at a time
        cout << counter << " sold ticket " << i << endl;
    }
}

int main() {
    thread a(announce, "Counter A");
    thread b(announce, "Counter B");
    a.join();
    b.join();
    return 0;
}

One run's output:

Counter A sold ticket 1
Counter A sold ticket 2
Counter A sold ticket 3
Counter B sold ticket 1
Counter B sold ticket 2
Counter B sold ticket 3

Quick quiz

Is this code safe?

int score = 0;
thread a([&] { score += 10; });
thread b([&] { score += 20; });
a.join();
b.join();
cout << score;
Answer

No, it's a data race. Both threads write score (captured by reference) with no mutex or atomic. It usually prints 30, which is exactly what makes it dangerous. Fix: atomic<int> score = 0; or lock a mutex around each +=.

Interview questions

  1. Process vs thread? A process is a running program with its own memory; threads are paths of execution inside one process that share its memory.
  2. What is a race condition? The result depends on the timing of threads; a data race is unsynchronised access to the same memory with at least one write (undefined behaviour in C++).
  3. Mutex vs atomic? An atomic makes single operations on one variable indivisible; a mutex protects a whole block of code and several variables.
  4. What is a deadlock and how do you avoid it? Threads each waiting for a lock another holds; lock in a fixed order, use scoped_lock, keep critical sections short.
  5. Why lock_guard instead of lock()/unlock()? RAII: it unlocks in its destructor, even on an early return or exception.
  6. join vs detach? join waits for the thread; detach lets it run on its own (you can no longer wait for it). A joinable thread's destructor calls terminate.
  7. What does std::async return? A std::future<T>; get() waits and returns the result or rethrows its exception.
  8. Concurrency vs parallelism? Concurrency: several tasks in progress, maybe taking turns on one core. Parallelism: really running at the same time on several cores.

Glossary

Term Meaning, with an example
thread One path of execution; main runs on the first one.
multithreading Running several threads in one program.
CPU core A part of the processor that runs one thread at a time; most laptops have 4-16.
std::thread Starts a function on a new thread: thread t(makeChai); (<thread>).
join() Waits until a thread finishes.
joinable A thread that was started and not yet joined or detached.
detach() Lets a thread run on its own; you can't wait for it any more.
std::terminate Ends the program immediately; called for an unjoined thread.
sleep_for Pauses the current thread: this_thread::sleep_for(seconds(1));
steady_clock A clock for measuring durations (<chrono>).
shared data A variable that several threads use, like booked.
race condition A result that depends on which thread is faster.
data race Two threads access the same memory, at least one writes, with no synchronisation: undefined behaviour.
undefined behaviour Anything can happen; the compiler makes no promises.
mutex Mutual exclusion: a lock only one thread can hold at a time (<mutex>).
lock / unlock Take / give back a mutex.
lock_guard Locks a mutex when created, unlocks when destroyed (RAII).
critical section The code that runs while a lock is held.
atomic A type whose operations are indivisible: atomic<int> booked; (<atomic>).
deadlock Threads waiting for each other's locks forever.
scoped_lock Locks several mutexes at once without deadlock (C++17).
std::async Runs a function, possibly on a new thread, and returns a future (<future>).
future<T> A result that will be ready later; get() waits for it.
launch::async Asks async to start a new thread right away.

Summary

  • A program runs on one thread unless you start more.
  • thread t(f, args...) runs f in parallel; always join() it, or the program terminates.
  • Shared data written by several threads is a race: wrong, different every run, and no error message.
  • Protect it with mutex + lock_guard, or use atomic for a single counter.
  • Avoid deadlock: one lock order, or scoped_lock.
  • async + future::get() run work in parallel and bring the result back.

Next episode: every program so far was one file. Real projects have hundreds. Build basics: compiling, linking and CMake.