std::thread::swap(3)

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

🌸 NAME 🌸

std::thread::swap - std::thread::swap. okay bestie, this is the page for std::thread::swap(3), fresh from the C++ Standard Libary manual, and it's lowkey iconic β€” this page ate and left no crumbs. The name is literally the function name twice, because the manual said so, and we respect the manual. πŸ’…

πŸ¦‹ Synopsis πŸ¦‹

void swap( std::thread& other ) noexcept;  (since C++11)

Exchanges the underlying handles of two thread objects. That's the whole move, no cap. Vibe check: passed. ✨

πŸ’– Parameters πŸ’–

  • other β€” the thread to swap with. That's it, bestie; no hidden extra args, no surprise lore. πŸ‘‘

πŸŽ€ Return value πŸŽ€

(none) β€” literally nothing gets returned. It's giving void energy, and we're obsessed. 🫢

πŸ”₯ Example πŸ”₯

Here's the example, exactly as it appears, because we do not play with code:

       // 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::thread t1(foo);
            std::thread 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';

            t1.join();
            t2.join();
        }

🌈 Possible output: 🌈

        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 also πŸ‘‘

  • std::swap(std::thread) β€” specializes the std::swap algorithm (C++11) (function). slay. πŸ¦‹

mwah πŸ’‹ this page came from http://cppreference.com, dated 2024.06.10, and it is the std::thread::swap(3) page. bye bestie, go swap those handles fr fr. periodt. 🌟

πŸ“¦ where this page comes from