Semaphores - PowerPoint PPT Presentation

About This Presentation
Title:

Semaphores

Description:

... more than one thread from accessing the same block of code at the same time, i.e. ... The P method subtracts one from the count and calls Monitor.Wait if ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 8
Provided by: jamesar3
Category:
Tags: ee | semaphores

less

Transcript and Presenter's Notes

Title: Semaphores


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
    gt 0.
  • The P method subtracts one from the count and
    calls Monitor.Wait if the count is lt 0.
  • The V method adds one to the count and calls
    Monitor.Pulse

7
Semaphore (contd)
  • If the count is initialized at 0, the semaphore
    is a so-called binary semaphore.
  • If the count is initialized with a count gt0, 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.
Write a Comment
User Comments (0)
About PowerShow.com