QSemaphore 类

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 ():

  • acquire( n ) 试着获得 n 资源。若没有那么多可用资源,调用将阻塞直到是这种情况。
  • release( n ) 释放 n 资源。

还有 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 ,和 信号量范例 .

成员函数文档编制

QSemaphore:: QSemaphore ( int n = 0)

Creates a new semaphore and initializes the number of resources it guards to n (by default, 0).

另请参阅 release () 和 available ().

QSemaphore:: ~QSemaphore ()

销毁信号量。

警告: Destroying a semaphore that is in use may result in undefined behavior.

void QSemaphore:: acquire ( int n = 1)

Tries to acquire n resources guarded by the semaphore. If n > available (), this call will block until enough resources are available.

另请参阅 release (), available (),和 tryAcquire ().

int QSemaphore:: available () const

Returns the number of resources currently available to the semaphore. This number can never be negative.

另请参阅 acquire () 和 release ().

void QSemaphore:: release ( int n = 1)

发行 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 .

bool QSemaphore:: tryAcquire ( int n = 1)

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 ().

bool QSemaphore:: tryAcquire ( int n , int timeout )

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 ().