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() | |
| 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) |
详细说明
在任何原本需要使用 `QSemaphore::release()` 的场合,都可以使用 `QSemaphoreReleaser`。构造一个 `QSemaphoreReleaser` 会将信号量的 `release()` 调用推迟到 `QSemaphoreReleaser` 被销毁时(参见RAII 模式)。
您可以利用此方法可靠地释放信号量,从而在发生异常或提前返回时避免死锁:
// ... do something that may throw or return early
sem.release();如果在调用sem.release() 之前发生了提前返回或抛出异常,则信号量不会被释放,这可能会导致在相应的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 default] 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)
交换*this 和other 的职责。
与移动赋值不同,在交换操作中,这两个对象中的任何一个都不会因此释放其信号量(如有)。
因此,该函数运行速度极快,且绝不会失败。
[noexcept] QSemaphoreReleaser &QSemaphoreReleaser::operator=(QSemaphoreReleaser &&other)
移动赋值运算符。从other 接管调用QSemaphore::release()的责任,而 则被取消。
如果该信号量释放器本身负有调用某些QSemaphore::release() 的责任,则会在从other 接管之前执行该调用。
另请参阅 cancel()。
© 2026 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.