std::unique_lock or std::lock_guard?

后端 未结 7 583
天命终不由人
天命终不由人 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 14:58

    Use lock_guard unless you need to be able to manually unlock the mutex in between without destroying the lock.

    In particular, condition_variable unlocks its mutex when going to sleep upon calls to wait. That is why a lock_guard is not sufficient here.

提交回复
热议问题