std::unique_lock or std::lock_guard?

后端 未结 7 581
天命终不由人
天命终不由人 2020-11-29 14:17

I have two use cases.

A. I want to synchronise access to a queue for two threads.

B. I want to synchronise access to a queue for two threads and use a conditio

7条回答
  •  情话喂你
    2020-11-29 15:14

    lock_guard and unique_lock are pretty much the same thing; lock_guard is a restricted version with a limited interface.

    A lock_guard always holds a lock from its construction to its destruction. A unique_lock can be created without immediately locking, can unlock at any point in its existence, and can transfer ownership of the lock from one instance to another.

    So you always use lock_guard, unless you need the capabilities of unique_lock. A condition_variable needs a unique_lock.

提交回复
热议问题