std::thread Joining and Detaching
Spawn threads with std::thread and choose between join and detach.
The std::thread Class
The <thread> header introduced threading to the standard library in C++11. Each std::thread instance represents one OS thread.
Spawning a Thread
Pass a callable (function, lambda, functor) to the constructor. The thread starts running immediately.
#include <thread>
#include <iostream>
void worker() { std::cout << "running on thread\n"; }
int main() {
std::thread t(worker);
t.join(); // wait for it to finish
}All lessons in this course
- std::thread Joining and Detaching
- std::mutex lock_guard and unique_lock
- Condition Variables and Worker Patterns
- std::async and std::future Basics