QSystemSemaphore 类提供系统信号量的一般计数。 更多...
| 头: | #include <QSystemSemaphore> |
| qmake: | QT += core |
| Since: | Qt 4.4 |
该类在 Qt 4.4 引入。
| enum | AccessMode { Open, Create } |
| enum | SystemSemaphoreError { NoError, PermissionDenied, KeyError, AlreadyExists, NotFound, …, 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) |
信号量是互斥的一般化。互斥只可以锁定一次,而信号量可以多次获取。通常,信号量用于保护一定数量的恒等资源。
像其更轻搭档 QSemaphore ,可以访问 QSystemSemaphore 从多个 threads 。不像 QSemaphore ,还可以访问 QSystemSemaphore 从多个 processes 。这意味着 QSystemSemaphore 是更重的类,因此,若应用程序不需要跨多个进程访问信号量,则可能想要使用 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 ().