|
| 1 | +.. _librt-threading: |
| 2 | + |
| 3 | +librt.threading |
| 4 | +=============== |
| 5 | + |
| 6 | +The ``librt.threading`` module is part of the ``librt`` package on PyPI, and it includes |
| 7 | +threading primitives. |
| 8 | + |
| 9 | +Classes |
| 10 | +------- |
| 11 | + |
| 12 | +Lock |
| 13 | +^^^^ |
| 14 | + |
| 15 | +.. class:: Lock |
| 16 | + |
| 17 | + A fast mutual exclusion lock. This can be used as a faster replacement for |
| 18 | + :py:class:`threading.Lock` in compiled code. |
| 19 | + |
| 20 | + Like :py:class:`threading.Lock`, a ``Lock`` is *unowned*: it may be released by a thread |
| 21 | + other than the one that acquired it, and it doesn't support reentrant (recursive) locking. |
| 22 | + A newly created lock is unlocked. |
| 23 | + |
| 24 | + ``Lock`` can be used as a context manager. The lock is acquired (blocking) on entry and |
| 25 | + released on exit, including when the body raises an exception:: |
| 26 | + |
| 27 | + def example(lock: Lock) -> None: |
| 28 | + with lock: |
| 29 | + ... # Critical section; the lock is held here. |
| 30 | + |
| 31 | + ``Lock`` cannot be subclassed. ``Lock`` cannot be used with :py:class:`threading.Condition`. |
| 32 | + |
| 33 | + .. method:: acquire(blocking: bool = True) -> bool |
| 34 | + |
| 35 | + Acquire the lock. |
| 36 | + |
| 37 | + When *blocking* is true (the default), block (if needed) until the lock is available, |
| 38 | + acquire it, and return ``True``. When *blocking* is false, acquire the lock only if it |
| 39 | + can be done without blocking: return ``True`` if the lock could be acquired, or |
| 40 | + ``False`` otherwise (it was already locked by some thread). |
| 41 | + |
| 42 | + Unlike :py:meth:`threading.Lock.acquire`, there is no *timeout* argument. |
| 43 | + |
| 44 | + .. method:: release() -> None |
| 45 | + |
| 46 | + Release the lock, allowing another thread (if any) that is blocked on :meth:`acquire` |
| 47 | + to proceed. Since the lock is unowned, it may be released from a thread other than the |
| 48 | + one that acquired it. |
| 49 | + |
| 50 | + Raise :py:exc:`RuntimeError` if the lock is not currently held. |
| 51 | + |
| 52 | + .. method:: locked() -> bool |
| 53 | + |
| 54 | + Return ``True`` if the lock is currently held (by any thread). |
0 commit comments