std::thread::join(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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_occur if this->get_id() == std::this_thread::get_id() (deadlock detected).
  • no_such_process if the thread is not valid.
  • invalid_argument if joinable() 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)
  • joinable context β€” 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