std::scoped...:scoped_lock(3)
π 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. Ifsizeof...(MutexTypes) == 0, it literally does nothing (iconic minimalism, honestly). Otherwise, ifsizeof...(MutexTypes) == 1, it effectively callsm.lock(). Otherwise β like when you've got a whole friend group of mutexes β it effectively callsstd::lock(m...). group-chat lock acquisition, slay. π₯(2)β theadopt_lock_tone. It acquires ownership of the mutexesm...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 bylock,try_lock,try_lock_for, ortry_lock_until) on each object inm.... 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 ascoped_lock, periodt. π«
β οΈ also, the behavior is undefined ifmis destroyed before thescoped_lockobject 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 whateverMutexTypessays). π¦
π Exceptions π
(1)β Throws any exceptions thrown bym.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_tparameter 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
- 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