Semaphores, mutexes and condition variables

advertisement
Semaphores, mutexes and
condition variables
semaphores
• Two types
– Binary – 0 or 1
– Counting 0 to n
• Wait – decrements
• > 0 forces a wait
• Post or signal – increments
• Lock/unlock – no restrictions
mutexes
• Owned by a thread
– Must be unlocked by the locker
– May be locked/unlocked in separate functions
• Attempts to get a locked mutex force caller to
sleep
Condition variables
• Creates a safe environment for testing
• Forces sleep when false
• Operation
– Lock a mutex
– Test condition (values in condition must be
protected by the mutex)
• If true, proceed with execution
• If false, the mutex is released for you and you are put to
sleep on the condition variable
• Any thread changing the conditions, signals the
condition variable, wakes up ONE sleeper
• You must reevaluate the condition or your program
may fail (could have changed before your tslice)
Download
Related flashcards
Create Flashcards