Mutex is a basic lock that allows for one thread at a time, also known as mutual exclusion.
Disable interrupts are necessary when using a Mutex.
Mutex locks variables.
Mutex: A basic lock that allows for one thread at a time, enforcing mutual exclusion.
Disable interrupts: Mutex requires the CPU to disable interrupts.
Lock variables: Mutex can lock variables.
Peterson’s Solution: Two threads can be synchronized using Peterson’s Solution which adds another variable.
Atomic test and set (TST): Performs both the test and set operations in a single instruction.
Kernel provided mutex: The kernel still needs to lock critical sections, such as disable interrupts and spinlocks.
Semaphores: A mutex superset that maintains a counter.
“Down” or “P”: Decrements the counter if it's positive or blocks the caller.
“Up” or “V”: Unblocks a process, if available, or increments the counter.
Producer/Consumer Model: This model is very common in computing, where a producer adds data to a fixed size queue at a particular rate, and a consumer removes and processes data.
Manage buffer capacity with two semaphores: “remaining” item semaphore (init 0) and “available” slot semaphore (init buffer size).
Producer decrements remaining semaphore before adding to queue, and increments available semaphore when done.
Consumer decrements available semaphore before removing from queue, and increments remaining semaphore when done.
Note that, depending on buffer implementation, additional locking may be necessary to protect the data structure.