Conditional Variable vs Semaphore

前端 未结 6 493
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 09:00

When should one use a semaphore and when should one use a conditional variable (CondVar) ?

6条回答
  •  醉梦人生
    2020-11-27 09:44

    Semaphores can be used to implement exclusive access to variables, however they are meant to be used for synchronization. Mutexes, on the other hand, have a semantics which is strictly related to mutual exclusion: only the process which locked the resource is allowed to unlock it.

    Unfortunately you cannot implement synchronization with mutexes, that's why we have condition variables. Also notice that with condition variables you can unlock all the waiting threads in the same instant by using the broadcast unlocking. This cannot be done with semaphores.

提交回复
热议问题