PySide6.QtCore.QSemaphoreReleaser¶
- class QSemaphoreReleaser¶
The
QSemaphoreReleaserclass provides exception-safe deferral of arelease()call.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QSemaphoreReleasercan be used wherever you would otherwise userelease(). Constructing aQSemaphoreReleaserdefers the release() call on the semaphore until theQSemaphoreReleaseris destroyed (see RAII pattern ).You can use this to reliably release a semaphore to avoid dead-lock in the face of exceptions or early returns:
# ... do something that may throw or return early sem.release()
If an early return is taken or an exception is thrown before the
sem.release()call is reached, the semaphore is not released, possibly preventing the thread waiting in the correspondingsem.acquire()call from ever continuing execution.When using RAII instead:
releaser = QSemaphoreReleaser(sem) # ... do something that may throw or early return # implicitly calls sem.release() here and at every other return in between
this can no longer happen, because the compiler will make sure that the
QSemaphoreReleaserdestructor is always called, and therefore the semaphore is always released.QSemaphoreReleaseris move-enabled and can therefore be returned from functions to transfer responsibility for releasing a semaphore out of a function or a scope:{ // some scope QSemaphoreReleaser releaser # does nothing # ... if someCondition: releaser = QSemaphoreReleaser(sem) # ... # ... } // conditionally calls sem.release(), depending on someCondition
A
QSemaphoreReleasercan be canceled by a call tocancel(). A canceled semaphore releaser will no longer callrelease()in its destructor.See also
QMutexLockerSynopsis¶
Methods¶
def
__init__()def
cancel()def
semaphore()def
swap()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- __init__()¶
Default constructor. Creates a
QSemaphoreReleaserthat does nothing.- __init__(sem[, n=1])
- Parameters:
sem –
QSemaphoren – int
Constructor. Stores the arguments and calls
sem.release(n) in the destructor.- __init__(sem[, n=1])
- Parameters:
sem –
QSemaphoren – int
Constructor. Stores the arguments and calls
sem->release(n) in the destructor.- cancel()¶
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Cancels this
QSemaphoreReleasersuch that the destructor will no longer callsemaphore()->release(). Returns the value ofsemaphore()before this call. After this call,semaphore()will returnNone.To enable again, assign a new
QSemaphoreReleaser:releaser.cancel() # avoid releasing old semaphore() releaser = QSemaphoreReleaser(sem, 42) # now will call sem.release(42) when 'releaser' is destroyed
- semaphore()¶
- Return type:
Returns a pointer to the
QSemaphoreobject provided to the constructor, or by the last move assignment, if any. Otherwise, returnsNone.- swap(other)¶
- Parameters:
other –
QSemaphoreReleaser
Exchanges the responsibilities of
*thisandother.Unlike move assignment, neither of the two objects ever releases its semaphore, if any, as a consequence of swapping.
Therefore this function is very fast and never fails.