std::thread::get_id(3)
πΈ std::thread::get_id(3) πΈ
okay so bestie, welcome to the <pink>std::thread::get_id</pink> page β it's filed under the C++ Standard Libary manual (that's literally how the header spells it, not me, iykyk π
) and it's short, sweet, and honestly kinda iconic.
β¨ NAME β¨
std::thread::get_id β literally std::thread::get_id. that's the whole name. no notes. π
π¦ Synopsis π¦
std::thread::id get_id() const noexcept;
(since C++11)
what it does: returns a value of std::thread::id identifying the thread associated with *this. that's it. that's the vibe check. β¨
π Parameters π
(none) β nothing to pass in, bestie. it takes zero arguments, it's low maintenance, we love that for it. π«Ά
π Return value π
a value of type std::thread::id identifying the thread associated with *this.
and here's the important part π: if there is no thread associated, then a default constructed std::thread::id is returned. so like... the id of nobody. a ghost id. ππ
π Example π
run this code, bestie:
#include <chrono>
#include <iostream>
#include <thread>
void foo()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::thread t1(foo);
std::thread::id t1_id = t1.get_id();
std::thread t2(foo);
std::thread::id t2_id = t2.get_id();
std::cout << "t1's id: " << t1_id << '\n';
std::cout << "t2's id: " << t2_id << '\n';
t1.join();
t2.join();
std::cout << "t1's id after join: " << t1.get_id() << '\n';
std::cout << "t2's id after join: " << t2.get_id() << '\n';
}
β Possible output β
t1's id: 140146221688576
t2's id: 140146213295872
t1's id after join: thread::id of a non-executing thread
t2's id after join: thread::id of a non-executing thread
see how the ids change after join()? that's the ghost-id situation from the Return value section showing up to the party π§ β once the threads are joined, get_id() hands you back a non-executing thread id. slay. π
π₯ See also π₯
idβ represents the id of a thread (public member class) πjoinableβ checks whether the thread is joinable, i.e. potentially running in parallel (public member function) π¦
and that's the whole page! periodt β¨ source + version + date for the record: http://cppreference.com, 2024.06.10, std::thread::get_id(3) β mwah, catch you in the next manpage bestie ππΈπ
π¦ 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