QSystemSemaphore 类

QSystemSemaphore 类提供常规计数系统信号量。 更多...

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

公共类型

enum AccessMode { Open, Create }
enum SystemSemaphoreError { NoError, PermissionDenied, KeyError, AlreadyExists, NotFound, …, UnknownError }

公共函数

QSystemSemaphore (const QNativeIpcKey & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)
QSystemSemaphore (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)
~QSystemSemaphore ()
bool acquire ()
QSystemSemaphore::SystemSemaphoreError error () const
QString errorString () const
QString key () const
QNativeIpcKey nativeIpcKey () const
bool release (int n = 1)
void setKey (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)
void setNativeKey (const QNativeIpcKey & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)

详细描述

A system semaphore is a generalization of QSemaphore . Typically, a semaphore is used to protect a certain number of identical resources.

Like its lighter counterpart, a QSystemSemaphore can be accessed from multiple threads 。不像 QSemaphore ,还可以访问 QSystemSemaphore 从多个 processes 。这意味着 QSystemSemaphore 是更重的类,因此,若应用程序不需要跨多个进程访问信号量,则可能想要使用 QSemaphore .

信号量支持 2 基础操作 acquire () 和 release ():

acquire () tries to acquire one resource. If there isn't a resource available, the call blocks until a resource becomes available. Then the resource is acquired and the call returns.

release () releases one resource so it can be acquired by another process. The function can also be called with a parameter n > 1, which releases n resources.

System semaphores are identified by a key, represented by QNativeIpcKey . A key can be created in a cross-platform manner by using platformSafeKey(). A system semaphore is created by the QSystemSemaphore constructor when passed an access mode parameter of AccessMode::Create. Once it is created, other processes may attach to the same semaphore using the same key and an access mode parameter of AccessMode::Open.

范例:创建系统信号量

QSystemSemaphore sem(QSystemSemaphore::platformSafeKey("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
					

For details on the key types, platform-specific limitations, and interoperability with older or non-Qt applications, see the Native IPC Keys documentation. That includes important information for sandboxed applications on Apple platforms, including all apps obtained via the Apple App Store.

另请参阅 进程间通信 , QSharedMemory ,和 QSemaphore .

成员类型文档编制

enum QSystemSemaphore:: AccessMode

This enum is used by the constructor and 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.

enum QSystemSemaphore:: SystemSemaphoreError

常量 描述
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 发生其它事情且很糟糕。

成员函数文档编制

QSystemSemaphore:: QSystemSemaphore (const QNativeIpcKey & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)

Requests a system semaphore for the specified key 。参数 initialValue and mode are used according to the following rules, which are system dependent.

In Unix, if the mode is 打开 and the system already has a semaphore identified by key , that semaphore is used, and the semaphore's resource count is not changed, i.e., initialValue is ignored. But if the system does not already have a semaphore identified by key , it creates a new semaphore for that key and sets its resource count to initialValue .

In Unix, if the mode is 创建 and the system already has a semaphore identified by key , that semaphore is used, and its resource count is set to initialValue . If the system does not already have a semaphore identified by key , it creates a new semaphore for that key and sets its resource count to initialValue .

In Windows, mode is ignored, and the system always tries to create a semaphore for the specified key . If the system does not already have a semaphore identified as key , it creates the semaphore and sets its resource count to initialValue . But if the system already has a semaphore identified as key it uses that semaphore and ignores initialValue .

The mode parameter is only used in Unix systems to handle the case where a semaphore survives a process crash. In that case, the next process to allocate a semaphore with the same key will get the semaphore that survived the crash, and unless mode is 创建 , the resource count will not be reset to initialValue but will retain the initial value it had been given by the crashed process.

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

QSystemSemaphore:: QSystemSemaphore (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)

Requests a system semaphore identified by the legacy key key .

[noexcept] QSystemSemaphore:: ~QSystemSemaphore ()

The destructor destroys the 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.

bool QSystemSemaphore:: acquire ()

Acquires one of the resources guarded by this semaphore, if there is one available, and returns true . If all the resources guarded by this semaphore have already been acquired, the call blocks until one of them is released by another process or thread having a semaphore with the same key.

If false is returned, a system error has occurred. Call error () to get a value of QSystemSemaphore::SystemSemaphoreError that indicates which error occurred.

另请参阅 release ().

QSystemSemaphore::SystemSemaphoreError QSystemSemaphore:: error () const

返回指示是否发生错误的值,且若如此,指示错误是什么。

另请参阅 errorString ().

QString QSystemSemaphore:: errorString () const

返回最后发生错误的文本描述。若 error () 返回 错误值 ,调用此函数以获取描述错误的文本字符串。

另请参阅 error ().

QString QSystemSemaphore:: key () const

Returns the legacy key assigned to this system semaphore. The key is the name by which the semaphore can be accessed from other processes.

另请参阅 setKey ().

QNativeIpcKey QSystemSemaphore:: nativeIpcKey () const

Returns the key assigned to this system semaphore. The key is the name by which the semaphore can be accessed from other processes.

You can use the native key to access system semaphores that have not been created by Qt, or to grant access to non-Qt applications. See Native IPC Keys 了解更多信息。

另请参阅 setNativeKey ().

bool QSystemSemaphore:: release ( int n = 1)

发行 n resources guarded by the semaphore. Returns true unless there is a system error.

Example: Create a system semaphore having five resources; acquire them all and then release them all.

QSystemSemaphore sem(QSystemSemaphore::platformSafeKey("market"), 5, QSystemSemaphore::Create);
for (int i = 0; i < 5; ++i)  // acquire all 5 resources
    sem.acquire();
sem.release(5);              // release the 5 resources
					

This function can also "create" resources. For example, immediately following the sequence of statements above, suppose we add the statement:

sem.release(10);          // "create" 10 new resources
					

Ten new resources are now guarded by the semaphore, in addition to the five that already existed. You would not normally use this function to create more resources.

另请参阅 acquire ().

void QSystemSemaphore:: setKey (const QString & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key initialValue and mode parameters are as defined for the constructor.

另请参阅 QSystemSemaphore () 和 key ().

void QSystemSemaphore:: setNativeKey (const QNativeIpcKey & key , int initialValue = 0, QSystemSemaphore::AccessMode mode = Open)

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key initialValue and mode parameters are as defined for the constructor.

This function is useful if the native key was shared from another process. See Native IPC Keys 了解更多信息。

另请参阅 QSystemSemaphore () 和 nativeIpcKey ().