QSemaphoreReleaser Class

The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call. More...

Header: #include <QSemaphoreReleaser>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Note: All functions in this class are reentrant.

Detailed Description

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 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 corresponding sem.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 between

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:

 { // some scope
     QSemaphoreReleaser releaser; // does nothing
     // ...
     if (someCondition) {
         releaser = QSemaphoreReleaser(sem);
         // ...
     }
     // ...
 } // conditionally calls sem.release(), depending on someCondition

A QSemaphoreReleaser can be canceled by a call to cancel(). A canceled semaphore releaser will no longer call QSemaphore::release() in its destructor.

See also QMutexLocker.