std::lock_guard vs std::unique_lock vs std::scoped_lock

So many C++ locking mechanisms… which one should you choose?

CMP
4 min readJun 16, 2023

--

If you’ve ever written concurrent code in C++ or any other language, then you know how difficult it can be to properly protect access to resources without causing data races or deadlocks. C++11 and C++17 have introduced several great locking mechanisms to help programmers write concurrent code, but which one(s) should you use in your own code?

--

--

CMP

Software engineer specializing in operating systems, navigating the intracicies of the C++ language and systems programming.