QSemaphoreReleaser 类

QSemaphoreReleaser 类提供异常安全延迟为 QSemaphore::release () 调用。 更多...

头: #include <QSemaphoreReleaser>
qmake: QT += core
Since: Qt 5.10

该类在 Qt 5.10 引入。

注意: 此类的所有函数 可重入 .

公共函数

QSemaphoreReleaser (QSemaphoreReleaser && other )
QSemaphoreReleaser (QSemaphore * sem , int n = 1)
QSemaphoreReleaser (QSemaphore & sem , int n = 1)
QSemaphoreReleaser ()
QSemaphoreReleaser & operator= (QSemaphoreReleaser && other )
~QSemaphoreReleaser ()
QSemaphore * cancel ()
QSemaphore * semaphore () const
void swap (QSemaphoreReleaser & other )

详细描述

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.

另请参阅 QMutexLocker .

成员函数文档编制

QSemaphoreReleaser:: QSemaphoreReleaser ( QSemaphoreReleaser && other )

Move constructor. Takes over responsibility to call QSemaphore::release () 从 other , which in turn is canceled.

另请参阅 cancel ().

QSemaphoreReleaser:: QSemaphoreReleaser ( QSemaphore * sem , int n = 1)

Constructor. Stores the arguments and calls sem ->release( n ) in the destructor.

QSemaphoreReleaser:: QSemaphoreReleaser ( QSemaphore & sem , int n = 1)

Constructor. Stores the arguments and calls sem .release( n ) in the destructor.

QSemaphoreReleaser:: QSemaphoreReleaser ()

Default constructor. Creates a QSemaphoreReleaser that does nothing.

QSemaphoreReleaser &QSemaphoreReleaser:: operator= ( QSemaphoreReleaser && other )

Move assignment operator. Takes over responsibility to call QSemaphore::release () 从 other , which in turn is canceled.

If this semaphore releaser had the responsibility to call some QSemaphore::release () itself, it performs the call before taking over from other .

另请参阅 cancel ().

QSemaphoreReleaser:: ~QSemaphoreReleaser ()

Unless canceled, calls QSemaphore::release () with the arguments provided to the constructor, or by the last move assignment.

QSemaphore *QSemaphoreReleaser:: cancel ()

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 () 会返回 nullptr .

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
					

QSemaphore *QSemaphoreReleaser:: semaphore () const

返回指针指向 QSemaphore object provided to the constructor, or by the last move assignment, if any. Otherwise, returns nullptr .

void QSemaphoreReleaser:: swap ( QSemaphoreReleaser & other )

Exchanges the responsibilites 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.