Why: Mutex#
-
class Mutex#
A mutual exclusion lock with priority inheritance support.
The Mutex class provides thread-safe mutual exclusion for protecting shared resources. This implementation uses priority inheritance to prevent priority inversion problems in real-time systems.
Priority Inheritance:
Priority inheritance is a mechanism that temporarily elevates the priority of a thread holding a mutex to match the priority of the highest-priority thread waiting for that mutex. This prevents the classic priority inversion scenario where a high-priority thread is blocked waiting for a low-priority thread to release a mutex, while medium-priority threads prevent the low-priority thread from running.
When a high-priority thread blocks on this mutex, the thread currently holding the lock inherits the higher priority, allowing it to complete its critical section more quickly and release the lock. Once the mutex is released, the thread’s priority returns to its original value.
This makes the Mutex suitable for use in real-time systems where predictable scheduling behavior is critical.
Public Functions
-
Mutex()#
-
~Mutex()#
-
void lock()#
Lock the mutex, blocking the caller until it is available. Not safe to call from an ISR!
-
void unlock()#
Unlock the mutex, unlocking one (if any) waiter
Note that, although unlocking does not block the caller (the unlocker), its use from an ISR is undefined because a mutex has an owner - the thread that successfully locked it. Depending on the underlying OS configuration, the OS might check or not.
-
Mutex()#