Threading
Mutex class - Threading
Mutex constructor - Threading
// PROTOTYPE
Mutex()
Construct a new Mutex object. You will typically include a Mutex
either as a member of your class.
In theory, it would be handy to make Mutex
a superclass of your class. Having the lock()
and unlock()
methods would make it work with WITH_LOCK()
. However, you must not do this if your class will ever be instantiated as a global variable. The reason is that the Mutex constructor calls os_mutex_create
, and this is not safe during global object construction. If you use a singleton that's only instantiated during setup or later, that would be safe.
Mutex functions cannot be called from an ISR.
Mutex::lock - Threading
// PROTOTYPE
void lock();
Lock the mutex. If the mutex is already locked, blocks the current thread until is is unlocked.
A Mutex
can only be locked once, even from the same thread. If you need to be able to lock a mutex multiple times, you should use RecursiveMutex
instead.
You cannot lock a mutex from an ISR.
Mutex::trylock - Threading
// PROTOTYPE
bool trylock();
bool try_lock();
Attempts to lock the mutex If it is unlocked, it will be locked and true
is returned. If it is already locked, false
is returned and the thread that previously held the lock will continue to hold the lock.
You cannot lock a mutex from an ISR.
Mutex::unlock - Threading
// PROTOTYPE
void unlock();
Unlocks the mutex. Typically only the thread that locked the mutex will unlock it.
You cannot unlock a mutex from an ISR.