std::unique_lock::swap(3)

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

πŸ’… std::unique_lock::swap(3)

okay bestie, this is the C++ Standard Libary page for std::unique_lock::swap, and it’s lowkey iconic because it’s all about swapping lock state with zero drama. πŸ’–

🌸 NAME 🌸

std::unique_lock::swap - std::unique_lock::swap

That’s literally the name bestie β€” no extra lore. 🫢

✨ Synopsis ✨

void swap( unique_lock& other ) noexcept;  (since C++11)

It exchanges the internal states of the lock objects. Like, the locks trade places internally β€” vibe check passed. πŸ”„βœ¨

πŸŽ€ Parameters πŸŽ€

  • other β€” the lock to swap the state with.

πŸ’… Return value πŸ’…

(none) β€” yep, it returns nothing, babe. Not a single value. 😌

πŸ¦‹ Example πŸ¦‹

// Run this code

#include <iostream>
#include <mutex>

int main()
{
    std::mutex mtx1;
    std::unique_lock<std::mutex> guard1(mtx1);
    std::unique_lock<std::mutex> guard2;
    guard2.swap(guard1);

    if (!guard1 && guard2)
        std::cout << "swapped success\n";

    return 0;
}

🌈 Output 🌈

swapped success

πŸ‘‘ See also πŸ‘‘

  • std::swap(std::unique_lock) β€” specialization of std::swap for unique_lock; (C++11) (function template).

okay bye bestie β€” this page is from cppreference.com (http://cppreference.com), version/date 2024.06.10, manual page std::unique_lock::swap(3). go swap something safely πŸ’–πŸ«Ά

πŸ“¦ where this page comes from