std::thread::join(3)
π
std::thread::join(3) β C++ Standard Libary π
πΈ NAME πΈ
std::thread::join β yes bestie, itβs literally std::thread::join, the C++ Standard Library member function that makes one thread wait for another to finish. Iconic waiting-room energy. πͺβ¨
β¨ Synopsis β¨
void join(); (since C++11)
Blocks the current thread until the thread identified by *this finishes its execution.
The completion of the thread identified by *this synchronizes with the corresponding successful return from join().
No synchronization is performed on *this itself. Concurrently calling join() on the same thread object from multiple threads constitutes a data race that results in undefined behavior. π¨ Like, do not do that, bestie. That is not the slay weβre looking for. π
π§΅ Parameters π§΅
(none) β literally zero parameters. She takes no arguments. Lowkey minimalist queen. π
π Return value π
(none) β returns nothing. Not even a tiny vibe. π«Ά
π Postconditions π
joinable() is false. Thatβs the post-join() vibe check, and it passed. β¨
π¨ Exceptions π¨
std::system_error if an error occurs.
π₯ Error conditions π₯
resource_deadlock_would_occurifthis->get_id() == std::this_thread::get_id()(deadlock detected).no_such_processif the thread is not valid.invalid_argumentifjoinable()is false.
π¦ Example π¦
// Run this code π
#include <chrono>
#include <iostream>
#include <thread>
void foo()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
π₯οΈ Output π₯οΈ
starting first helper...
starting second helper...
waiting for helpers to finish...
done!
π References π
- C++23 standard (ISO/IEC 14882:2023):
- 33.4.3.6 Members [thread.thread.member]
- C++20 standard (ISO/IEC 14882:2020):
- 32.4.2.5 Members [thread.thread.member]
- C++17 standard (ISO/IEC 14882:2017):
- 33.3.2.5 thread members [thread.thread.member]
- C++14 standard (ISO/IEC 14882:2014):
- 30.3.1.5 thread members [thread.thread.member]
- C++11 standard (ISO/IEC 14882:2011):
- 30.3.1.5 thread members [thread.thread.member]
π See also π
detachβ permits the thread to execute independently from the thread handle (public member function)joinablecontext β checks whether the thread is joinable, i.e. potentially running in parallel (public member function)- C documentation for
thrd_join
okay bestie, thatβs the whole page β source/version: http://cppreference.com, date: 2024.06.10 ππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms