QSemaphore 类提供通用计数信号量。 更多...
头: | #include <QSemaphore> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
注意: 此类的所有函数 thread-safe .
QSemaphore (int n = 0) | |
~QSemaphore () | |
void | acquire (int n = 1) |
int | available () const |
void | release (int n = 1) |
bool | tryAcquire (int n = 1) |
bool | tryAcquire (int n , int timeout ) |
信号量是互斥的泛化。尽管互斥只可以被锁定一次,但多次获得信号量是可能的。信号量通常用于保护一定数量的恒等资源。
信号量支持 2 基础操作 acquire () 和 release ():
还有 tryAcquire () 函数若无法获得资源就立即返回,和 available () 函数可随时返回可用资源数。
范例:
QSemaphore sem(5); // sem.available() == 5 sem.acquire(3); // sem.available() == 2 sem.acquire(2); // sem.available() == 0 sem.release(5); // sem.available() == 5 sem.release(5); // sem.available() == 10 sem.tryAcquire(1); // sem.available() == 9, returns true sem.tryAcquire(250); // sem.available() == 9, returns false
信号量的典型应用程序是用于控制由生产者线程和消费者线程共享的循环缓冲的访问。 信号量范例 展示如何使用 QSemaphore 去解决该问题。
信号量的非计算范例是在餐馆用餐。采用餐厅中的椅子数初始信号量。当人们到达时,他们想要座位。由于座无虚席, available () 递减。当人们离开时, available () 增加,允许更多人进入。若要坐 10 人的宴会,却只有 9 个席位,这 10 人会等待,但要坐 4 人的宴会 (把可用座位增加到 5,使 10 人的宴会等待更长时间)。
另请参阅 QSemaphoreReleaser , QMutex , QWaitCondition , QThread ,和 信号量范例 .
Creates a new semaphore and initializes the number of resources it guards to n (by default, 0).
另请参阅 release () 和 available ().
销毁信号量。
警告: Destroying a semaphore that is in use may result in undefined behavior.
Tries to acquire
n
resources guarded by the semaphore. If
n
>
available
(), this call will block until enough resources are available.
另请参阅 release (), available (),和 tryAcquire ().
Returns the number of resources currently available to the semaphore. This number can never be negative.
发行 n resources guarded by the semaphore.
This function can be used to "create" resources as well. For example:
QSemaphore sem(5); // a semaphore that guards 5 resources sem.acquire(5); // acquire all 5 resources sem.release(5); // release the 5 resources sem.release(10); // "create" 10 new resources
QSemaphoreReleaser 是 RAII wrapper around this function.
另请参阅 acquire (), available (),和 QSemaphoreReleaser .
Tries to acquire
n
resources guarded by the semaphore and returns
true
on success. If
available
() <
n
, this call immediately returns
false
without acquiring any resources.
范例:
QSemaphore sem(5); // sem.available() == 5 sem.tryAcquire(250); // sem.available() == 5, returns false sem.tryAcquire(3); // sem.available() == 2, returns true
另请参阅 acquire ().
Tries to acquire
n
resources guarded by the semaphore and returns
true
on success. If
available
() <
n
, this call will wait for at most
timeout
milliseconds for resources to become available.
注意:传递负数作为 timeout 相当于调用 acquire (), i.e. this function will wait forever for resources to become available if timeout 为负。
范例:
QSemaphore sem(5); // sem.available() == 5 sem.tryAcquire(250, 1000); // sem.available() == 5, waits 1000 milliseconds and returns false sem.tryAcquire(3, 30000); // sem.available() == 2, returns true without waiting
另请参阅 acquire ().