On this page
  1. std::thread
  2. Overview
  3. Notes & Pitfalls
  4. Example

std::thread

Prev: std::mutex & std::scoped_lock | Up: concurrency | Next: std::atomic
Rel: std::jthread
Ref: cppreference — std::thread
Tags: #public #prog #cpp #stdlib #concurrency #tier/standard


Overview

std::thread (<thread>, C++11) wraps an OS thread of execution. Unlike std::jthread, if a joinable std::thread is destroyed without calling join() or detach(), the C++ runtime immediately invokes std::terminate().

Notes & Pitfalls

Example

#include <cassert>
#include <functional>
#include <thread>

int main() {
    int result = 0;
    std::thread t([](int& out) { out = 99; }, std::ref(result));
    t.join();
    assert(result == 99);
}

Linked from

Related notes