notes

Personal notes
git clone git://git.laack.co/notes.git
Log | Files | Refs

ConditionVariables.md (1071B)


      1 # Condition Variables
      2 
      3 **Source:** CS 6200
      4 
      5 **Chapter:** P2L2
      6 
      7 **Definition:** Condition variables are used with [mutexes](Mutex.md), and provide the ability to condition waits.
      8 
      9 ## Example
     10 
     11 An example of this would be waiting for a list to be full to print it. You would first acquire the lock to print and call Wait(m, listFull). The listFull condition variable will be set to true once the insertion logic completes, filling up the list. Note that the Wait function must unlock the mutex and re-aquire the lock once the condition variable is true because insertion and printing are assumed to be using the same mutex for accessing the list.
     12 
     13 A real implementation would likely use Signal(listFull) to send the message to the waiting logic that it may proceed.
     14 
     15 ## API
     16 
     17 Common API
     18 
     19 - Condition type
     20     - list of waiting threads
     21     - reference to mutex
     22     - ...
     23 - Wait(mutex, cond)
     24     - mutex automatically locked and unlocked while waiting for cond
     25 - Signal(cond)
     26     - notify only one thread waiting on condition
     27 - Broadcast(cond)
     28     - notify all waiting threads