Monitor vs Mutex

前端 未结 3 1955
感动是毒
感动是毒 2021-01-30 21:08

I read that mutex is a semaphore with value 1 (binary semaphore) used to enforce mutual exclusion.

I read this link Semaphore vs. Monitors - what's the difference? wh

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 21:36

    Monitor are different then Mutex but they can be considered similar in a sense that Monitor are build on top of Mutex. See depiction of monitor in an image at the bottom, for clarity.

    Monitor is a synchronization construct that allows threads to have both mutual exclusion (using locks) and cooperation i.e. the ability to make threads wait for certain condition to be true (using wait-set).

    In other words, along with data that implements a lock, every Java object is logically associated with data that implements a wait-set. Whereas locks help threads to work independently on shared data without interfering with one another, wait-sets help threads to cooperate with one another to work together towards a common goal e.g. all waiting threads will be moved to this wait-set and all will be notified once lock is released. This wait-set helps in building monitors with additional help of lock (mutex).

    I you want, you can see my answer here, which may or may not be relevant to this question.

    You can find another relevant discussion here

    Semaphore vs. Monitors - what's the difference?

提交回复
热议问题