top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is monitors ? How are they different from semaphores?

+1 vote
277 views
What is monitors ? How are they different from semaphores?
posted May 31, 2014 by Amit Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

A monitor is a set of multiple routines which are protected by a mutex. Any routines in the monitor can be executed by a thread by acquiring a lock and make sure that only one thread can execute within the monitor at a time. A thread can suspend itself inside a monitor and then wait for an event to occur. If this happens, then another thread is given the opportunity to enter the monitor. Suspended thread gets notified that the event it was waiting for has now occurred, after which thread acquire the lock.

Where as semaphore is also a similar mechanism but simpler then a monitor because it’s just a lock that protects a shared resource (not a set of routines as in case of monitor). The thread must get the lock before using that shared resource protected by a semaphore.

Example of a Semaphore – a Mutex

Now coming to when these are used, both are used for thread synchronization. But, monitors are simpler than semaphores because they handle all of the details of lock acquisition and release. An thread using semaphores has to release any locks a thread has acquired when it terminates – this must be done by the application itself. If the application does not do this, then any other thread that needs the shared resource will not be able to proceed. Another difference when using semaphores is that every routine accessing a shared resource has to explicitly acquire a a lock before using the resource.

answer May 31, 2014 by Chahat Sharma
...