QSemaphoreReleaser Class

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

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

注意:该类中的所有函数都是可重入的

公共函数

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

详细说明

QSemaphoreReleaser 可用于任何需要使用QSemaphore::release() 的地方。构造 QSemaphoreReleaser 会推迟对 semaphore 的 release() 调用,直到 QSemaphoreReleaser 被销毁(参见RAII 模式)。

面对异常或提前返回,您可以使用它来可靠地释放一个 semaphore,以避免死锁:

// ... do something that may throw or return early
sem.release();

如果在到达sem.release() 调用之前提前返回或抛出异常,则不会释放 Semaphore,从而可能导致在相应的sem.acquire() 调用中等待的线程无法继续执行。

而使用 RAII 时

const QSemaphoreReleaser releaser(sem);
// ... do something that may throw or early return
// implicitly calls sem.release() here and at every other return in between

这种情况就不会再发生了,因为编译器会确保 QSemaphoreReleaser 析构函数始终被调用,因此信号寄存器始终被释放。

QSemaphoreReleaser 支持移动,因此可以从函数中返回,将释放信号传递器的责任转移到函数或作用域之外:

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

调用cancel() 可以取消 QSemaphoreReleaser。被取消的信号释放器将不再在其析构函数中调用QSemaphore::release() 。

另请参阅 QMutexLocker

成员函数文档

[constexpr noexcept] QSemaphoreReleaser::QSemaphoreReleaser()

默认构造函数。创建一个什么都不做的 QSemaphoreReleaser。

[explicit noexcept] QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore &sem, int n = 1)

构造函数。在析构函数中存储参数并调用sem.release(n)。

[explicit noexcept] QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore *sem, int n = 1)

构造函数。存储参数,并在析构函数中调用sem->release(n)。

[noexcept] QSemaphoreReleaser::QSemaphoreReleaser(QSemaphoreReleaser &&other)

移动构造函数。从other 接管调用QSemaphore::release() 的责任,而 () 则被取消。

另请参阅 cancel() 。

[noexcept] QSemaphoreReleaser::~QSemaphoreReleaser()

除非被取消,否则将使用构造函数提供的参数或最后一次移动赋值调用QSemaphore::release() 。

[noexcept] QSemaphore *QSemaphoreReleaser::cancel()

取消QSemaphoreReleaser ,析构函数不再调用semaphore()->release() 。返回调用前semaphore() 的值。调用后,semaphore() 将返回nullptr

要再次启用,请分配一个新的QSemaphoreReleaser

releaser.cancel(); // avoid releasing old semaphore()
releaser = QSemaphoreReleaser(sem, 42);
// now will call sem.release(42) when 'releaser' is destroyed

[noexcept] QSemaphore *QSemaphoreReleaser::semaphore() const

返回指向向构造函数提供的QSemaphore 对象的指针,或最后一次移动赋值的指针(如果有)。否则,返回nullptr

[noexcept] void QSemaphoreReleaser::swap(QSemaphoreReleaser &other)

交换*thisother 的职责。

与移动赋值不同的是,两个对象都不会因为交换而释放自己的 Semaphore(如果有的话)。

因此,该函数运行速度非常快,而且从不出错。

[noexcept] QSemaphoreReleaser &QSemaphoreReleaser::operator=(QSemaphoreReleaser &&other)

移动任务操作员。从other 接管调用QSemaphore::release() 的责任,而 () 则被取消。

如果该信号释放器本身有责任调用某个QSemaphore::release() ,它将在接替other 之前执行调用。

另请参阅 cancel()。

© 2025 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.