mutex — mutex module¶
The mutex module provides a mutual-exclusion primitive for protecting
critical sections of code or shared data from concurrent access by an interrupt
service routine and the main thread.
Lock acquisition and release is performed via a context manager (with
statement), which blocks until the mutex is available. A non-blocking
Mutex.test() method is also provided.
Example:
from mutex import Mutex
mtx = Mutex()
# Acquire for a critical section (blocks if already held).
with mtx:
# ... protected code, e.g. shared buffer access ...
pass
# Or try without blocking.
if mtx.test():
try:
# ... protected code ...
pass
finally:
mtx.release()
class Mutex – mutex object¶
- class mutex.Mutex¶
Creates an unlocked mutex object.
- release() None¶
Unlock the mutex. Raises
mutex.MutexExceptionif the mutex is not currently locked.
Exceptions¶
- exception mutex.MutexException¶
Subclass of
OSError. Raised byMutex.releasewhen the mutex is already unlocked.