std::thread Joining and Detaching
Spawn threads with std::thread and choose between join and detach.
std::thread Joining and Detaching is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
}Passing Arguments
Forward arguments to the callable through the constructor.
std::thread t([](int n) { std::cout << n; }, 42);
t.join();Passing by Reference
Use std::ref to pass arguments by reference. Without it, the thread receives copies.
int counter = 0;
std::thread t([](int& c) { ++c; }, std::ref(counter));
t.join();
std::cout << counter; // 1join(): Wait for Completion
Call join() to block the calling thread until the target finishes. After join, the thread is no longer joinable.
detach(): Run Independently
Call detach() to run the thread independently. It continues running after the std::thread is destroyed. Resources are released by the OS when the thread exits.
std::thread t([]() { /* background work */ });
t.detach(); // we no longer track itjoinable()
Check whether the thread can be joined or detached. A thread that has been joined or detached is no longer joinable.
Forgetting to Join: Termination
If a std::thread is destroyed while still joinable, the program calls std::terminate. Always join or detach before destruction.
RAII Wrapper
Write a small RAII wrapper that joins in the destructor. Or use std::jthread (C++20) — it joins automatically.
#include <thread>
// C++20
std::jthread t([] { /* work */ });
// t joins automatically when destroyedHardware Concurrency
Query the number of hardware threads with std::thread::hardware_concurrency(). Use it to size thread pools.
Thread IDs
Each thread has a unique ID. Get the current thread s ID with std::this_thread::get_id().
Sleeping a Thread
Use std::this_thread::sleep_for(duration) to pause the current thread.
#include <chrono>
using namespace std::chrono_literals;
std::this_thread::sleep_for(500ms);Quick Check
What happens if a joinable std::thread is destroyed before join() or detach() is called?
Recap
Spawn threads with std::thread and a callable. Always join() or detach() before the thread object is destroyed — or use std::jthread (C++20) which auto-joins.
Frequently asked questions
Is the “std::thread Joining and Detaching” lesson free?
Yes — the full text of “std::thread Joining and Detaching” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “std::thread Joining and Detaching”?
Spawn threads with std::thread and choose between join and detach. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “std::thread Joining and Detaching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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