operating-systemsystemmutexsemaphoremonitors

Do semaphores have conditional variables?


Aren't wait and signal conditional variables to signify request and release?

This link states that semaphores do not have coniditonal variables while monitors do.

According to the same site,

The conditional variable allows a process to wait inside the monitor and allows a waiting process to resume immediately when the other process releases the resources.

Isn't that the same procedure in a semaphore?


Solution

  • The difference here is that semaphore is a stateful object, while condition variable is stateless.

    The idea is that sometime you have a very complex state (that cannot be represented by a simple counter like a semaphore) and you want to wait for that state to change. That is the reason why condition variables are used with mutexes - a mutex is required to protect the change of that state and allows waiting for a change without losing notifications.

    Internally some semaphore implementations are based on condition variables - in this case counter is a protected state that is going to change. But such implementations are not very effecient as modern OS have better ways to implement semaphores.

    If you want to know how condition variables and semaphores can be implemented, you can read my answer here.