1 / 7

Semaphores

Semaphores. Another Means Of Thread Synchronization. Lock.

Télécharger la présentation

Semaphores

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Semaphores Another Means Of Thread Synchronization

  2. Lock • We have seen what the lock statement does. It prevents more than one thread from accessing the same block of code at the same time, i.e., it lets the thread holding the lock complete the locked block before being preempted by another thread.

  3. Monitor • Monitor.Enter() • Monitor.Exit() • Can be used to lock a block of code providing an object reference is used.

  4. Monitor.Wait() • Releases the lock on an object and blocks the current thread until it reacquires the lock.

  5. Monitor.Pulse() • Only the current owner of the lock can signal a waiting object using Pulse. • The thread that currently owns the lock on the specified object invokes this method to signal the next thread in line for the lock. Upon receiving the pulse, the waiting thread is moved to the ready queue. When the thread that invoked Pulse releases the lock, the next thread in the ready queue (which is not necessarily the thread that was pulsed) acquires the lock. • Note that a synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state

  6. Semaphore • The semaphore is initialized with a count that is >= 0. • The P method subtracts one from the count and calls Monitor.Wait if the count is < 0. • The V method adds one to the count and calls Monitor.Pulse

  7. Semaphore (cont’d) • If the count is initialized at 0, the semaphore is a so-called binary semaphore. • If the count is initialized with a count >0, the semaphore is a counting semaphore. The initial count gives the number of threads that can take out units from this semaphore before blocking occurs.

More Related