std::thread::swap(3)
πΈ 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 thestd::swapalgorithm (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
- 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