Monitors and Semaphores are to accomplish the same task, to ensure that between n processes/threads that each enter their critical section atomically. Although Monitors go towards a Object Ordinate Approach to this, making the code easier to read for example. In this case a Monitor is at a higher level then a Semaphore. You can use a Semaphore to implement a Monitor. In that case a Monitor has a counter
variable in the same way a Semaphore does.
The V and P of a Semaphore is very much like the wait and signal of a Monitor. In which wait and signal are used on a Monitors condition variables. They can be used to implement the same functionality of a Semaphore. Even though there are some minor differences, such as for signal with no process in need of being unblocked there is no affect on the condition variable. Along with that locks are also used within the procedure.
While in most cases you don't want to. You can use a Monitors condition variables and lock outside of it (monitor.lock
,monitor.condition_i
), basically doing the normal things we do within a procedure but on the outside, not within it's function. This is assuming you want to have the critical section outside of the monitor, otherwise there is no need in doing this:
// monitor enter
monitor.lock.acquire();
while(...) monitor.condition_i.wait( monitor.lock );
runCriticalSection();
// monitor exit
monitor.condition_j.signal();
monitor.lock.release();