std::thread
std::thread starts a new OS thread that runs a function you pass to the constructor. You must join or detach it before the std::thread object is destroyed, or std::terminate is called.
Detailed Explanation
A thread is a separate call stack and a program counter the OS schedules. std::thread t(fn, args...) creates one and starts fn(args...) on it. Arguments are copied (or moved) into storage the new thread owns. To pass a reference, wrap it in std::ref. To pass a move-only type, std::move it in.
The std::thread object is a handle, not the OS thread itself. It is move-only. After you move from it, joinable() is false and the destination owns the handle.
A joinable handle must be joined or detached before destruction. join() blocks until the thread function returns. detach() lets the thread run without a handle; you can no longer wait for it, and if it touches locals of the launching function those locals must still be alive. Detach is rarely the right default. Join is.
If a joinable std::thread destructor runs, the standard calls std::terminate. That is why a thread in a scope that can throw needs RAII (std::jthread, or a wrapper that joins in the destructor). C++20 jthread does that and adds stop tokens.
joinable() is true if the handle represents a thread that has not been joined or detached. Default-constructed thread{} is not joinable. After join(), it is not joinable.
Exceptions in the thread function that leave the function call std::terminate unless you catch them inside the thread. There is no automatic transport to the launcher. Use std::promise / std::future or a thread-safe queue if you need an error back.
std::this_thread::sleep_for, yield, and get_id are the this-thread helpers. std::thread::hardware_concurrency() is a hint for how many workers to start, and may return 0.
Data races are on you. Two threads writing int x without a mutex or atomic is undefined behaviour. See data-race.
Prefer a thread pool or std::async when you want a result, not a lifetime you manage. Prefer jthread when you want RAII join and cooperative cancel.
Code Examples
#include <iostream>
#include <thread>
#include <vector>
void fill(std::vector<int>& out, int n) {
out.push_back(n);
}
int main() {
std::vector<int> out;
std::thread t(fill, std::ref(out), 7);
t.join(); // wait; out is now safe to read
std::cout << out[0] << '\n'; // prints: 7
}#include <chrono>
#include <iostream>
#include <thread>
int main() {
std::thread t([] {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::cout << "done\n";
});
t.join(); // prints: done
}Common Gotchas
Destroying a joinable `thread`
`std::terminate`. Join, detach, or use `jthread`.
Passing a local by reference and detaching
the thread runs after the local dies. Use `std::ref` only when you `join` before the local goes out of scope.
Arguments are copied
`std::thread t(fn, vec)` copies `vec`. Move if you are done with it: `std::thread t(fn, std::move(vec))`.
Uncaught exceptions in the thread function terminate the process
catch at the root of the thread, or use `packaged_task` / `async`.