std::scoped...:scoped_lock(3)

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

πŸ’– std::scoped_lock::scoped_lock β€” NAME energy ✨

okay so bestie, this page lives in the C++ Standard Libary manual, section std::scoped...:scoped_lock(3), and it's literally all about the constructors of std::scoped_lock::scoped_lock. yes the NAME is the same thing twice, that's just how these pages roll, it's giving "tell me you're a constructor without telling me you're a constructor" πŸ’…


🌸 Synopsis 🌸

explicit scoped_lock( MutexTypes&... m );           (1) (since C++17)
scoped_lock( std::adopt_lock_t, MutexTypes&... m ); (2) (since C++17)
scoped_lock( const scoped_lock& ) = delete;         (3) (since C++17)

So what's the whole vibe? It acquires ownership of the given mutexes m. that's the entire personality. 🫢

Here's the breakdown, no cap:

  • (1) β€” the normal one. If sizeof...(MutexTypes) == 0, it literally does nothing (iconic minimalism, honestly). Otherwise, if sizeof...(MutexTypes) == 1, it effectively calls m.lock(). Otherwise β€” like when you've got a whole friend group of mutexes β€” it effectively calls std::lock(m...). group-chat lock acquisition, slay. πŸ”₯
  • (2) β€” the adopt_lock_t one. It acquires ownership of the mutexes m... without attempting to lock any of them. it's taking your word for it. it's not checking. πŸ™ˆ
⚠️ big warning bestie: for (2), the behavior is undefined unless the current thread holds a non-shared lock (i.e., a lock acquired by lock, try_lock, try_lock_for, or try_lock_until) on each object in m.... every single one. don't be out here adopting locks you don't actually hold, that's a one-way ticket to undefined-behavior town πŸ’€
  • (3) β€” the copy constructor is deleted. not available. gone. you cannot copy a scoped_lock, periodt. 🚫
⚠️ also, the behavior is undefined if m is destroyed before the scoped_lock object is. the mutexes have to outlive the lock. that's the rule, bestie. the scoped_lock is the main character, but the mutexes gotta stay on stage the whole show. πŸ‘‘

πŸŽ€ Parameters πŸŽ€

  • m β€” mutexes to acquire ownership of. the whole squad. the more the merrier (well, up to whatever MutexTypes says). πŸ¦‹

πŸ’œ Exceptions πŸ’œ

  • (1) β€” Throws any exceptions thrown by m.lock(). if locking throws, this throws. sharing is caring. 😭
  • (2) β€” Throws nothing. absolutely nothing. cool, calm, collected, zero drama. ✨

🧚 Defect reports 🧚

so this is the "glow-up receipts" section. the following behavior-changing defect reports were applied retroactively to previously published C++ standards (yes, they went back and fixed history, lowkey iconic):

  • DR: P0739R0
  • Applied to: C++17
  • Behavior as published: the adopt_lock_t parameter was last, impeding class template argument deduction
  • Correct behavior: moved to first

basically the adopt_lock_t was sitting at the back of the bus and it was messing with class template argument deduction, so they moved it to the front. glow-up complete. πŸ’…β­


and that's the page bestie, from http://cppreference.com 🌈 β€” version date 2024.06.10 β€” go forth and lock responsibly πŸ’–πŸŽ€πŸ¦‹πŸ“

πŸ“¦ where this page comes from