Skip to main content

Synchronization

Producer and Consumer with Semaphores

  • Easy to implement, one sema for consumer and producer
  • But not the solution we are approaching to? There are some ambiguity on whether the semaphore puts the threads to sleep or is forced to spin-wait (thus makes it busy-waiting and not recommended)
Producer waits for the vending machine to be empty (&emptySlots semaphore > 0)
waits for the machine to be free, lock it up (&mutex)
enqueue
release the machine (release &mutex)
up sema for fullslots

Consumer waits for the vending to be fully? slotted (&fullSlots semaphore > 0)
waits for the machine to be free, lock it up (&mutex)
dequeue
release the machine (release &mutex)
up sema for empty

*mutex semaphores ensures critical section integrity

Producer and Consumer with monitors

  • Monitor is a lock with zero or more conditional variables, and conditional variables are a queue of threads.