std::jthread::swap(3)
π std::jthread::swap β the glow-up exchange
okay bestie, welcome to the std::jthread::swap page from the C++ Standard Library manual, dated 2024.06.10 and sourced from cppreference.com β we'll get to the cute sign-off at the end, don't worry π
β¨ NAME β¨
std::jthread::swap β yes queen, the name is literally just <rainbow>std::jthread::swap</rainbow>, it's giving "I'm the main character and my name is my name" π
πΈ Synopsis πΈ
so here's the signature, and it's short and iconic:
void swap( std::jthread& other ) noexcept; (since C++20)
what does it do? it exchanges the underlying handles of two jthread objects. that's it. that's the whole vibe. two threads swap their little souls, no cap β¨
and yes, it's noexcept β it will not throw at you, it's built different π«Ά
π¦ Parameters π¦
otherβ thejthreadto swap with π your swap buddy. your thread bestie. the one you're trading handles with.
π Return value π
none. she gives nothing back and honestly? respect. void energy. π
π₯ Example π₯
okay so here's the whole demo, verbatim, don't @ me:
// Run this code
#include <chrono>
#include <iostream>
#include <thread>
void foo()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::jthread t1(foo);
std::jthread t2(bar);
std::cout << "thread 1 id: " << t1.get_id() << '\n'
<< "thread 2 id: " << t2.get_id() << '\n';
std::swap(t1, t2);
std::cout << "after std::swap(t1, t2):" << '\n'
<< "thread 1 id: " << t1.get_id() << '\n'
<< "thread 2 id: " << t2.get_id() << '\n';
t1.swap(t2);
std::cout << "after t1.swap(t2):" << '\n'
<< "thread 1 id: " << t1.get_id() << '\n'
<< "thread 2 id: " << t2.get_id() << '\n';
}
so like, t1 runs foo, t2 runs bar, we print the ids, we std::swap them, print again, then we call t1.swap(t2) directly and print one more time. bestie is showing you BOTH ways β the std::swap algorithm route and the member function route π
Possible output π
it's random-ish ids so like, don't panic if yours look different, but here's what it can look like:
thread 1 id: 140185268262656
thread 2 id: 140185259869952
after std::swap(t1, t2):
thread 1 id: 140185259869952
thread 2 id: 140185268262656
after t1.swap(t2):
thread 1 id: 140185268262656
thread 2 id: 140185259869952
see how they flip-flop? first swap trades them, second swap trades them right back π the ids are literally boomeranging. that's the entire point and it ate π₯
π See also π
swap(std::jthread)β specializes thestd::swapalgorithm (C++20) (function) β like the member one but the free-function version, and yes, she's got her own page iykyk
ngl the whole page is like, four lines of actual content plus a demo, and I'm obsessed with the efficiency. no fluff. no filler. just vibes and handle exchange πΈ
and that's a wrap, bestie π«Ά this was std::jthread::swap(3) from the C++ Standard Library manpage, published 2024.06.10, straight from http://cppreference.com β go swap something iconic today, mwah πβ¨
π¦ 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