QSemaphore 類

QSemaphore 類提供通用計數信號量。 更多...

頭: #include <QSemaphore>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此類的所有函數 綫程安全 .

公共函數

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 )
bool tryAcquire (int n , std::chrono::duration<Rep, Period> timeout )
bool try_acquire ()
bool try_acquire_for (const std::chrono::duration<Rep, Period> & timeout )
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
					

信號量的典型應用程序是用於控製由生産者綫程和消費者綫程共享的循環緩衝的訪問。 信號量範例 展示如何使用 QSemaphore 去解決該問題。

信號量的非計算範例是在餐館用餐。采用餐廳中的椅子數初始信號量。當人們到達時,他們想要座位。由於座無虛席, 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 (默認 0)。

另請參閱 release () 和 available ().

QSemaphore:: ~QSemaphore ()

銷毀信號量。

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

void QSemaphore:: acquire ( int n = 1)

試著獲得 n 由信號量守衛的資源。若 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 由信號量守衛的資源。

此函數還可以用於 "創建" 資源。例如:

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 包裹器圍繞此函數。

另請參閱 acquire (), available (),和 QSemaphoreReleaser .

bool QSemaphore:: tryAcquire ( int n = 1)

試著獲得 n 由信號量守衛的資源並返迴 true 當成功時。若 available () < n ,此調用立即返迴 false 未獲得任何資源。

範例:

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 )

試著獲得 n 由信號量守衛的資源並返迴 true 當成功時。若 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.3] template <typename Rep, typename Period> bool QSemaphore:: tryAcquire ( int n , std::chrono::duration < Rep , Period > timeout )

這是重載函數。

This function was introduced in Qt 6.3.

[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.

This function was introduced in 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.

This function was introduced in 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.

This function was introduced in Qt 6.3.

另請參閱 tryAcquire (), try_acquire (),和 try_acquire_for ().