std::jthread::join(3)
πΈ std::jthread::join(3) πΈ
okay bestie, welcome to the C++ Standard Libary manual β yes, that's the manual name, and we're keeping it cute and exact. This page is all about std::jthread::join, and it's lowkey iconic. π
π NAME π
std::jthread::join β std::jthread::join (that's the whole NAME, bestie, no extra lore π).
π¦ Synopsis π¦
void join(); (since C++20)
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 jthread object from multiple threads constitutes a data race that results in undefined behavior.
π¨ bestie, fr fr, that means: don't have multiple threads callingjoin()on the samejthreadobject at the same time. It's a data race, and data races are undefined behavior. Not cute. Not slay. π
π Parameters π
Parameters: none. Literally none, bestie. β¨
π Return value π
Return value: none. She returns void, periodt. π«Ά
β Postconditions β
joinable() is false. That's the postcondition, and it ate. β
π₯ 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()isfalse.
π 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::jthread helper1(foo);
std::cout << "starting second helper...\n";
std::jthread 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.4.3 Members [thread.jthread.mem]
- C++20 standard (
ISO/IEC 14882:2020):32.4.3.2 Members [thread.jthread.mem]
π§ See also π§
detachβ permits the thread to execute independently from the thread handle (public member function).joinableβ checks whether the thread is joinable, i.e. potentially running in parallel context (public member function).- C documentation for
thrd_join.
mwah π β source/version: http://cppreference.com; date: 2024.06.10; page: std::jthread::join(3). πβ¨
π¦ 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