Difference between mutex and semaphore

  • 1

    Mutex as its name suggests provides complete mutual exclusion. At any point of time only one thread can lock a particular mutex.

    Sempahore is a non negative integer used when we want more fine grained parallelism. Semaphores are useful when we do not need complete mutual exclusion but want only concurrency control.
    Sempahores can be illustrated by the following two major categories:

    1. Producer-Consumer problem where we need synchronization only occasionally(concurrency needed only when the producer is producing at a much higher rate than the consumer can consume or vice versa).

    2. Readers-Writers problem where multiple readers can read at any point of time but only one writer can change the data.

  • 0

    One point added is that if writing C program, we could use named semaphore in multiprocessing as long as all processes agree on a common name, but for mutex, we need to created a shared mutex in shared-memory.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.