Home c++ How does std :: unique_lock differ from std :: lock_guard?

How does std :: unique_lock differ from std :: lock_guard?

Author

Date

Category

How is std :: unique_lock different from std :: lock_guard? In what situations is it better to use one class and in what other?


Answer 1, authority 100%

lock_guard and unique_lock are pretty much the same. The difference is that you can lock and unlock std :: unique_lock . std :: lock_guard will lock only once on build and unlock on destruction.

  • lock_guard : when an object is created, it tries to acquire a mutex
    (by calling lock () ) and when the object is destroyed it is automatically
    frees the mutex (by calling unlock () ).
  • unique_lock : also supports deferred locking, temporary locking, recursive locking, and using conditional variables.

Source

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions