Thursday, September 01, 2005

critical sections - mutexes and semaphores

Mutex:
  1. Mutual Exclusion object that blocks simultaneous access to a shared resource.
  2. Locks a shared resource, if already being acessed. Any attempt by other thread to access the shared resource would simply block.
  3. Guards critical section i.e. part of code where shared resource is accessed and may be modified.
Semaphore:
  1. A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad :)
  2. Mutual Exclusion + Synchronisation + Signalling + Counting
  3. Can be used as mutex.
  4. Multiple threads if simultaneously try to access a shared resource, which they may modify, may be synchronized using a semaphore. When the first thread arrives and enters critical section then simultaneously if other threads also try to enter the critical section, they are sequentially queued (FIFO) into wait state by the semaphore object implementation. When the first thread in critical section would leave it, semaphore would update count, and signal next i.e. second thread to enter the critical section. Thus shared resource access is synchronized / serialized sequentially.
  5. Maybe slower than mutex, as extra implentation is involved.

0 Comments:

Post a Comment

<< Home