![]() When someone returns their bike then someone else can take it. If you try and hire a bike and they have one free then you can take it, otherwise you must wait. They lock the door to prevent anyone else coming in, do their stuff, and then unlock it when they leave.Ī semaphore is like a bike hire place. The thread that releases a semaphore need not be the same thread that acquired it.Ī monitor is like a public toilet. ![]() If multiple threads are waiting when a thread releases a semaphore then one of them gets it. If the counter is already zero when a thread tries to acquire the semaphore then it has to wait until another thread releases the semaphore. When a thread is done then it releases the semaphore, and increments the counter. When the counter is positive, if a thread tries to acquire the semaphore then it is allowed, and the counter is decremented. A semaphore essentially is just a counter. You might well use a semaphore to implement a monitor. If one thread is currently executing a member function of the object then any other thread that tries to call a member function of that object will have to wait until the first has finished.Ī Semaphore is a lower-level object. The member functions or methods of a monitor object will enforce mutual exclusion, so only one thread may be performing any action on the object at a given time. ![]() A Monitor is an object designed to be accessed from multiple threads.
0 Comments
Leave a Reply. |