Locks and keys
Busy waiting / spinning
Semaphore type
Structure of Semaphores
Semaphore Attribute: Value (int)
Semaphore Attribute: Queue
Semaphore Method: Initialize
Semaphore Method: P (or Wait, down)
Semaphore Method: V (or Signal, up)
P and V in C#
P as a method
semProducer.WaitOne(); called in method Produce
V as a method
semProducer.Release(); called in method Consume
Creating a Semaphore
Semaphore semProducer; // counts empty slots to write
Semaphore semConsumer; //counts full slots to read & remove
semProducer = new Semaphore(bufferSize, bufferSize) //initial & max units
Semaphore signal history
Use of semaphores
Semaphores in CS
Non-critical code
Get Semaphore obj - Wait
Critical code
Release Semaphore obj - Signal
Non-critical code
Several semaphores in a code block
Unblocking and blocking with VP
What are Counting semaphores good for?
What are binary semaphores good for?
How do Counting semaphores work?
How do Binary semaphores work?
Semaphore sem = new Semaphore(1)
sem -> P()
// CS
sem -> V()
But – any thread can use the methods. Recommended to use locks when locking critical sections.
What is a Mutex?
Mutex vs Lock