QSemaphoreReleaser¶
The
QSemaphoreReleaser
class provides exception-safe deferral of arelease()
call. More…
New in version 5.10.
Synopsis¶
Functions¶
Detailed Description¶
QSemaphoreReleaser
can be used wherever you would otherwise userelease()
. Constructing aQSemaphoreReleaser
defers the release() call on the semaphore until theQSemaphoreReleaser
is 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:
const QSemaphoreReleaser releaser(sem); // ... do something that may throw or early return // implicitly calls sem.release() here and at every other return in betweenthis can no longer happen, because the compiler will make sure that the
QSemaphoreReleaser
destructor is always called, and therefore the semaphore is always released.
QSemaphoreReleaser
is 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 someConditionA
QSemaphoreReleaser
can be canceled by a call tocancel()
. A canceled semaphore releaser will no longer callrelease()
in its destructor.See also
- class PySide2.QtCore.QSemaphoreReleaser¶
PySide2.QtCore.QSemaphoreReleaser(sem[, n=1])
PySide2.QtCore.QSemaphoreReleaser(sem[, n=1])
- param sem:
- param n:
int
Default constructor. Creates a
QSemaphoreReleaser
that does nothing.Constructor. Stores the arguments and calls
sem
->release(n
) in the destructor.
- PySide2.QtCore.QSemaphoreReleaser.cancel()¶
- Return type:
Cancels this
QSemaphoreReleaser
such 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
- PySide2.QtCore.QSemaphoreReleaser.semaphore()¶
- Return type:
Returns a pointer to the
QSemaphore
object provided to the constructor, or by the last move assignment, if any. Otherwise, returnsNone
.
- PySide2.QtCore.QSemaphoreReleaser.swap(other)¶
- Parameters:
Exchanges the responsibilities of
*this
andother
.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.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.