![]() The application must acquire the lock before using that shared resource protected by a semaphore.īoth Monitors and Semaphores are used for the same purpose – thread synchronization. If you have a bike then you can give it to someone else to return - the bike hire place doesn’t care who returns it, as long as they get their bike back.Ī lock that protects a shared resource (need to acquire the lock to access resource).Ī monitor is a set of multiple routines which are protected by a mutual exclusion lock whereas, A semaphore is a simpler construct than a monitor because it’s just a lock that protects a shared resource – and not a set of routines like a monitor. 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. (need to acquire lock to execute the single thread) SemaphoreĪ Semaphore is a lower-level object. It controls only ONE thread at a time can execute in the 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. 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 mutex is the same as a lock but it can be system wide (shared by multiple processes).Ī Monitor is an object designed to be accessed from multiple threads.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |