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