The QSystemSemaphore class provides a general counting system semaphore. 更多...
| 頭: | #include <QSystemSemaphore> |
| qmake: | QT += core |
| Since: | Qt 4.4 |
| enum | AccessMode { Open, Create } |
| enum | SystemSemaphoreError { NoError, PermissionDenied, KeyError, AlreadyExists, ..., UnknownError } |
| QSystemSemaphore (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open) | |
| ~QSystemSemaphore () | |
| bool | acquire () |
| QSystemSemaphore::SystemSemaphoreError | error () const |
| QString | errorString () const |
| QString | key () const |
| bool | release (int n = 1) |
| void | setKey (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open) |
The QSystemSemaphore class provides a general counting system semaphore.
信號量是互斥的一般化。互斥隻可以鎖定一次,而信號量可以多次獲取。通常,信號量用於保護一定數量的恒等資源。
像其更輕搭檔 QSemaphore , QSystemSemaphore can be accessed from multiple threads 。不像 QSemaphore , QSystemSemaphore can also be accessed from multiple processes 。這意味著 QSystemSemaphore is a much heavier class, so if your application doesn't need to access your semaphores across multiple processes, you will probably want to use QSemaphore .
信號量支持 2 基礎操作 acquire () 和 release ():
acquire () 試圖獲得 1 個資源。若沒有資源可用,調用阻塞直到資源變為可用。然後獲得資源並調用返迴。
release () 釋放 1 個資源,所以另一進程可以獲得它。函數也可以采用 n > 1 的參數調用,釋放 n 個資源。
A system semaphore is created with a string key that other processes can use to use the same semaphore.
範例:創建係統信號量
QSystemSemaphore sem("market", 3, QSystemSemaphore::Create); // resources available == 3 sem.acquire(); // resources available == 2 sem.acquire(); // resources available == 1 sem.acquire(); // resources available == 0 sem.release(); // resources available == 1 sem.release(2); // resources available == 3
A typical application of system semaphores is for controlling access to a circular buffer shared by a producer process and a consumer processes.
當使用此類時,要意識到以下平颱差異:
Windows: QSystemSemaphore does not own its underlying system semaphore. Windows owns it. This means that when all instances of QSystemSemaphore for a particular key have been destroyed, either by having their destructors called, or because one or more processes crash, Windows removes the underlying system semaphore.
Unix:
另請參閱 QSharedMemory and QSemaphore .
此枚舉用於構造函數和 setKey (). Its purpose is to enable handling the problem in Unix implementations of semaphores that survive a crash. In Unix, when a semaphore survives a crash, we need a way to force it to reset its resource count, when the system reuses the semaphore. In Windows, where semaphores can't survive a crash, this enum has no effect.
| 常量 | 值 | 描述 |
|---|---|---|
QSystemSemaphore::Open
|
0
|
If the semaphore already exists, its initial resource count is not reset. If the semaphore does not already exist, it is created and its initial resource count set. |
QSystemSemaphore::Create
|
1
|
QSystemSemaphore takes ownership of the semaphore and sets its resource count to the requested value, regardless of whether the semaphore already exists by having survived a crash. This value should be passed to the constructor, when the first semaphore for a particular key is constructed and you know that if the semaphore already exists it could only be because of a crash. In Windows, where a semaphore can't survive a crash, Create and Open have the same behavior. |
| 常量 | 值 | 描述 |
|---|---|---|
QSystemSemaphore::NoError
|
0
|
沒有齣現錯誤。 |
QSystemSemaphore::PermissionDenied
|
1
|
操作失敗,因為調用者沒有所需權限。 |
QSystemSemaphore::KeyError
|
2
|
操作失敗,因為鍵無效。 |
QSystemSemaphore::AlreadyExists
|
3
|
The operation failed because a system semaphore with the specified key already existed. |
QSystemSemaphore::NotFound
|
4
|
The operation failed because a system semaphore with the specified key could not be found. |
QSystemSemaphore::OutOfResources
|
5
|
The operation failed because there was not enough memory available to fill the request. |
QSystemSemaphore::UnknownError
|
6
|
發生其它事情且很糟糕。 |
請求係統信號量為指定 key 。參數 initialValue and mode 的使用根據下列規則,從屬係統。
在 Unix,若 mode is 打開 且係統已有信號量的標識是通過 key ,使用該信號量,且信號量的資源計數不改變,即 initialValue 被忽略。但是,若係統還沒有已標識信號量通過 key ,為該 key 創建新信號量,並將其資源計數設為 initialValue .
在 Unix,若 mode is 創建 且係統已有信號量的標識是通過 key ,使用該信號量,且其資源計數被設為 initialValue 。若係統還沒有已標識信號量通過 key ,為該 key 創建新信號量,並將其資源計數設為 initialValue .
在 Windows, mode 被忽略,且係統始終試著創建信號量為指定 key 。若係統還沒有信號量被標識為 key ,創建信號量並將其資源計數設為 initialValue 。但是,若係統已經有信號量被標識為 key 使用該信號量並忽略 initialValue .
The mode 參數僅用於 Unix 係統,處理進程崩潰幸存信號量的情況。在此情況下,分配信號量的下一進程采用相同 key 將獲取崩潰幸存信號量,且除非 mode is 創建 ,資源計數不會被重置為 initialValue 但會保留由崩潰進程賦予它的初始值。
析構函數銷毀 QSystemSemaphore object, but the underlying system semaphore is not removed from the system unless this instance of QSystemSemaphore is the last one existing for that system semaphore.
Two important side effects of the destructor depend on the system. In Windows, if acquire () has been called for this semaphore but not release (), release () will not be called by the destructor, nor will the resource be released when the process exits normally. This would be a program bug which could be the cause of a deadlock in another process trying to acquire the same resource. In Unix, acquired resources that are not released before the destructor is called are automatically released when the process exits.
獲得由此信號量守衛的 1 個資源 (若有 1 個可用),返迴
true
。若已獲得由此信號量守衛的所有資源,調用阻塞,直到它們之一被具有相同鍵的信號量的另一進程 (或綫程) 釋放。
若返迴 false,有齣現係統錯誤。調用 error () 以獲取值對於 QSystemSemaphore::SystemSemaphoreError 指示齣現瞭哪種錯誤。
另請參閱 release ().
返迴指示是否發生錯誤的值,且若如此,指示錯誤是什麼。
另請參閱 errorString ().
返迴最後發生錯誤的文本描述。若 error () 返迴 錯誤值 ,調用此函數以獲取描述錯誤的文本字符串。
另請參閱 error ().
返迴賦值給此係統信號量的鍵。鍵是可以從其它進程訪問的信號量名稱。
另請參閱 setKey ().
發行
n
由信號量守衛的資源。返迴
true
除非存在係統錯誤。
範例:創建擁有 5 個資源的係統信號量;獲得它們所有,然後釋放它們所有。
QSystemSemaphore sem("market", 5, QSystemSemaphore::Create); for (int i = 0; i < 5; ++i) // acquire all 5 resources sem.acquire(); sem.release(5); // release the 5 resources
此函數還可以 "創建" 資源。例如,緊接上文語句序列之後,假設添加語句:
sem.release(10); // "create" 10 new resources
現在新建由信號量守衛的 10 個資源,除已存在的 5 個外。通常不會使用此函數,來創建更多資源。
另請參閱 acquire ().
此函數的工作如同構造函數。它重構此 QSystemSemaphore 對象。若新的 key 不同於舊鍵,調用此函數就像采用舊鍵調用信號量析構函數,然後調用構造函數創建新信號量采用新 key 。 initialValue and mode 參數都是為構造函數定義的。
另請參閱 QSystemSemaphore () 和 key ().