std::jthread::swap(3)

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

πŸ’… 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 β€” the jthread to 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 the std::swap algorithm (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