QSemaphore 类

QSemaphore 类提供通用计数信号量。 更多...

头: #include <QSemaphore>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
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 )
(从 6.6 起) bool tryAcquire (int n , QDeadlineTimer timer )
(since 6.3) bool tryAcquire (int n , std::chrono::duration<Rep, Period> timeout )
(since 6.3) bool try_acquire ()
(since 6.3) bool try_acquire_for (const std::chrono::duration<Rep, Period> & timeout )
(since 6.3) bool try_acquire_until (const std::chrono::time_point<Clock, Duration> & tp )

详细描述

信号量是一般化的互斥。尽管互斥只可以锁定一次,但多次获得信号量是可能的。信号量通常用于保护一定数量的恒等资源。

信号量支持 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
					

信号量的典型应用程序是用于控制由生产者线程和消费者线程共享的循环缓冲的访问。 使用信号量的生产者和消费者 example shows how to use QSemaphore to solve that problem.

信号量的非计算范例是在餐馆用餐。采用餐厅中的椅子数初始信号量。当人们到达时,他们想要座位。由于座无虚席, available () 递减。当人们离开时, available () 增加,允许更多人进入。若要坐 10 人的宴会,却只有 9 个席位,这 10 人会等待,但要坐 4 人的宴会 (把可用座位增加到 5,使 10 人的宴会等待更长时间)。

另请参阅 QSemaphoreReleaser , QMutex , QWaitCondition , QThread ,和 使用信号量的生产者和消费者 .

成员函数文档编制

[explicit] QSemaphore:: QSemaphore ( int n = 0)

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

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

[noexcept] 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 ().

[since 6.6] bool QSemaphore:: tryAcquire ( int n , QDeadlineTimer timer )

Tries to acquire n resources guarded by the semaphore and returns true on success. If available () < n , this call will wait until timer expires for resources to become available.

范例:

QSemaphore sem(5);                          // sem.available() == 5
sem.tryAcquire(250, QDeadlineTimer(1000));  // sem.available() == 5, waits 1000 milliseconds and returns false
sem.tryAcquire(3, QDeadlineTimer(30s));     // sem.available() == 2, returns true without waiting
					

该函数在 Qt 6.6 引入。

另请参阅 acquire ().

[since 6.3] template <typename Rep, typename Period> bool QSemaphore:: tryAcquire ( int n , std::chrono::duration < Rep , Period > timeout )

这是重载函数。

该函数在 Qt 6.3 引入。

[noexcept, since 6.3] bool QSemaphore:: try_acquire ()

This function is provided for std::counting_semaphore compatibility.

It is equivalent to calling tryAcquire(1) , where the function returns true on acquiring the resource successfully.

该函数在 Qt 6.3 引入。

另请参阅 tryAcquire (), try_acquire_for (),和 try_acquire_until ().

[since 6.3] template <typename Rep, typename Period> bool QSemaphore:: try_acquire_for (const std::chrono::duration < Rep , Period > & timeout )

This function is provided for std::counting_semaphore compatibility.

It is equivalent to calling tryAcquire(1, timeout) , where the call times out on the given timeout value. The function returns true on acquiring the resource successfully.

该函数在 Qt 6.3 引入。

另请参阅 tryAcquire (), try_acquire (),和 try_acquire_until ().

[since 6.3] template <typename Clock, typename Duration> bool QSemaphore:: try_acquire_until (const std::chrono::time_point < Clock , Duration > & tp )

This function is provided for std::counting_semaphore compatibility.

It is equivalent to calling tryAcquire(1, tp - Clock::now()) , which means that the tp (time point) is recorded, ignoring the adjustments to Clock while waiting. The function returns true on acquiring the resource successfully.

该函数在 Qt 6.3 引入。

另请参阅 tryAcquire (), try_acquire (),和 try_acquire_for ().