![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call. More...
#include <qsemaphore.h>
Public Member Functions | |
Q_NODISCARD_CTOR | QSemaphoreReleaser ()=default |
Default constructor. | |
Q_NODISCARD_CTOR | QSemaphoreReleaser (QSemaphore &sem, int n=1) noexcept |
Constructor. | |
Q_NODISCARD_CTOR | QSemaphoreReleaser (QSemaphore *sem, int n=1) noexcept |
Constructor. | |
Q_NODISCARD_CTOR | QSemaphoreReleaser (QSemaphoreReleaser &&other) noexcept |
Move constructor. | |
~QSemaphoreReleaser () | |
Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor, or by the last move assignment. | |
void | swap (QSemaphoreReleaser &other) noexcept |
Exchanges the responsibilities of {*this} and other. | |
QSemaphore * | semaphore () const noexcept |
Returns a pointer to the QSemaphore object provided to the constructor, or by the last move assignment, if any. | |
QSemaphore * | cancel () noexcept |
Cancels this QSemaphoreReleaser such that the destructor will no longer call {semaphore()->release()}. | |
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
\inmodule QtCore
\reentrant
QSemaphoreReleaser can be used wherever you would otherwise use QSemaphore::release(). Constructing a QSemaphoreReleaser defers the release() call on the semaphore until the QSemaphoreReleaser is destroyed (see \l{http://en.cppreference.com/w/cpp/language/raii}{RAII pattern}).
You can use this to reliably release a semaphore to avoid dead-lock in the face of exceptions or early returns:
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 corresponding
{sem.acquire()} call from ever continuing execution.
When using RAII instead:
this 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:
A QSemaphoreReleaser can be canceled by a call to cancel(). A canceled semaphore releaser will no longer call QSemaphore::release() in its destructor.
Definition at line 66 of file qsemaphore.h.
|
default |
Default constructor.
Creates a QSemaphoreReleaser that does nothing.
|
inlineexplicitnoexcept |
Constructor.
Stores the arguments and calls {sem}.release({n}) in the destructor.
Definition at line 72 of file qsemaphore.h.
|
inlineexplicitnoexcept |
Constructor.
Stores the arguments and calls {sem}->release({n}) in the destructor.
Definition at line 75 of file qsemaphore.h.
|
inlinenoexcept |
Move constructor.
Takes over responsibility to call QSemaphore::release() from other, which in turn is canceled.
Definition at line 78 of file qsemaphore.h.
|
inline |
Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor, or by the last move assignment.
Definition at line 82 of file qsemaphore.h.
|
inlinenoexcept |
Cancels this QSemaphoreReleaser such that the destructor will no longer call {semaphore()->release()}.
Returns the value of semaphore() before this call. After this call, semaphore() will return \nullptr.
To enable again, assign a new QSemaphoreReleaser:
Definition at line 97 of file qsemaphore.h.
|
inlinenoexcept |
Returns a pointer to the QSemaphore object provided to the constructor, or by the last move assignment, if any.
Otherwise, returns \nullptr.
Definition at line 94 of file qsemaphore.h.
|
inlinenoexcept |
Exchanges the responsibilities of {*this} and other.
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.
Definition at line 88 of file qsemaphore.h.