Monitor vs Mutex vs Semaphore

Monitor - Same as lock block. Limited to single process. Light weight. Allows single thread at a time.

Mutex - Can be shared across processes. Heavy weight compared to Monitor. Allows single thread at a time.

Semaphore - Similar to Mutex but allows multiple threads at the same time which can be configured. A Semaphore with count 1 is Mutex.

I want to share a good example for Mutex & Semaphore which I learned when I was working in Infosys.

Mutex explained as, there is a toilet which has key and only one person can enter into that with the single key. Once the first person completes his task the key will be handed over to the next person. At a time only one person can use the toilet.

Semaphore explained as, there are multiple toilets with keys let's say 4. Simultaneously 4 persons can enter and use the toilets. Once any one of the toilet is vacant the next person enters into the vacant toilet. The count can be customized.