Threading Fundamentals Terminology Reference

This reference provides an overview of threading and concurrency terminology you'll encounter in this chapter. Use it as a quick lookup guide.

Core Concepts

Term Definition Example
Thread Independent unit of execution with its own instruction pointer and stack std::thread t(function);
Main Thread The thread that starts when program execution begins int main() runs on the main thread
Concurrency Structuring a program to handle multiple tasks Multiple downloads in progress simultaneously
Parallelism Actually executing multiple tasks at the same time Two threads running on different CPU cores
Thread of Execution A sequence of instructions that can execute independently Each std::thread represents a thread of execution

Thread Management

Term Definition Example
Join Block until a thread completes execution t.join();
Detach Separate thread from std::thread object, letting it run independently t.detach();
Joinable State indicating a thread can be joined or detached if (t.joinable()) t.join();
Daemon Thread A detached thread that runs in the background Background logging thread
Thread ID Unique identifier for each thread std::this_thread::get_id()

Thread Creation

Term Definition Example
Callable Any object that can be called like a function Function, lambda, functor
Functor A class with operator() defined class Worker { void operator()(); };
Most Vexing Parse Ambiguous syntax that declares function instead of object std::thread t(Worker()); is a function declaration

Thread Arguments

Term Definition Example
std::ref Creates a reference wrapper for passing by reference std::thread t(f, std::ref(x));
std::cref Creates a const reference wrapper std::thread t(f, std::cref(x));
Reference Wrapper Copyable object that behaves like a reference std::reference_wrapper<int>
Thread-Local Copy Copy of argument made for thread's internal storage Arguments are copied by default

Synchronization Concepts

Term Definition Example
Race Condition Bug where result depends on unpredictable execution order Two threads incrementing same counter
Data Race Unsynchronized access to shared data (undefined behavior) One thread reads while another writes
Deadlock Situation where threads wait for each other forever Thread A waits for B, B waits for A
Critical Section Code that accesses shared resources Code between lock and unlock

C++20 Threading Features

Term Definition Example
std::jthread Thread that auto-joins in destructor std::jthread t(function);
std::stop_token Token to check if stop was requested while (!stoken.stop_requested())
std::stop_source Object that can request a stop source.request_stop();
std::stop_callback Callback executed when stop is requested std::stop_callback cb(token, []{ cleanup(); });
Cooperative Cancellation Pattern where thread checks for stop requests Thread periodically checks stop_requested()

Hardware and Performance

Term Definition Example
Hardware Concurrency Number of threads hardware can run simultaneously std::thread::hardware_concurrency()
CPU-Bound Work Tasks that need lots of computation Image processing, mathematical calculations
I/O-Bound Work Tasks that spend time waiting for external resources File downloads, database queries
Context Switch OS switching between threads on a CPU core Overhead when too many threads

Thread Control

Term Definition Example
Sleep Pause thread execution for a duration std::this_thread::sleep_for(100ms);
Yield Hint to scheduler to let other threads run std::this_thread::yield();
Thread-Local Storage Variables with separate instances per thread thread_local int counter = 0;

Exception Safety

Term Definition Example
RAII Thread Guard Object that joins thread in destructor ThreadGuard guard(t);
std::terminate Called when joinable thread is destroyed Program crash if thread not joined/detached
Exception Propagation Exceptions don't cross thread boundaries Must handle exceptions within thread

Headers

Header Contents
<thread> std::thread, std::jthread, std::this_thread
<functional> std::ref, std::cref, std::reference_wrapper
<chrono> Duration types for sleep_for
<stop_token> std::stop_token, std::stop_source, std::stop_callback