Semaphores For Mutual Exclusion

Semaphores are things intended to pass signals between independent threads or processes. A process can wait on a semaphore: this means it is suspended until some other process signals the semaphore. If the semaphore has already been signaled, however, when the process waits, it doesn't actually suspend, but just continues right on through.

Semaphores for mutual exclusion are a sub-category of all semaphores. They are used to block access to a resource, usually. If you have a socket that only one process can use at a time, and you have multiple processes that use the socket, then each process can have code like this (pseudocode):

   socket_semaphore wait().
   code_to_use_socket().
   socket_semaphore signal().

Start all the processes and signal the semaphore once. One of the waiting processes will get to go; then it will signal the semaphore, and another process waiting will go; etc.


In VisualWorks Smalltalk, you can create a semaphore that already has the initial signal mentioned above, by the code Semaphore forMutualExclusion. --DanaAnthony


From: SynchronizationStrategies

CategoryConcurrencyPatterns


EditText of this page (last edited August 27, 2013) or FindPage with title or text search