QSharedMemory 类提供对共享内存段的访问。 更多...
头: | #include <QSharedMemory> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
继承: | QObject |
enum | AccessMode { ReadOnly, ReadWrite } |
enum | SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, AlreadyExists, …, UnknownError } |
QSharedMemory (const QString & key , QObject * parent = nullptr) | |
QSharedMemory (QObject * parent = nullptr) | |
QSharedMemory (const QNativeIpcKey & key , QObject * parent = nullptr) | |
virtual | ~QSharedMemory () |
bool | attach (QSharedMemory::AccessMode mode = ReadWrite) |
const void * | constData () const |
bool | create (qsizetype size , QSharedMemory::AccessMode mode = ReadWrite) |
void * | data () |
const void * | data () const |
bool | detach () |
QSharedMemory::SharedMemoryError | error () const |
QString | errorString () const |
bool | isAttached () const |
QString | key () const |
bool | lock () |
QNativeIpcKey | nativeIpcKey () const |
QString | nativeKey () const |
void | setKey (const QString & key ) |
void | setNativeKey (const QNativeIpcKey & key ) |
void | setNativeKey (const QString & key , QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs()) |
qsizetype | size () const |
bool | unlock () |
QSharedMemory provides access to a shared memory segment by multiple threads and processes. Shared memory segments are identified by a key, represented by QNativeIpcKey . A key can be created in a cross-platform manner by using platformSafeKey().
One QSharedMemory object must create () the segment and this call specifies the size of the segment. All other processes simply attach () to the segment that must already exist. After either operation is successful, the application may call data () to obtain a pointer to the data.
To support non-atomic operations, QSharedMemory provides API to gain exclusive access: you may lock the shared memory with lock () before reading from or writing to the shared memory, but remember to release the lock with unlock () 在完成后。
By default, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain.
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.
另请参阅 进程间通信 and QSystemSemaphore .
常量 | 值 | 描述 |
---|---|---|
QSharedMemory::ReadOnly
|
0
|
共享内存段是只读的。不允许写入共享内存段。尝试写入采用 ReadOnly 创建的共享内存段,会导致程序中止。 |
QSharedMemory::ReadWrite
|
1
|
允许读写共享内存段。 |
常量 | 值 | 描述 |
---|---|---|
QSharedMemory::NoError
|
0
|
没有出现错误。 |
QSharedMemory::PermissionDenied
|
1
|
操作失败,因为调用者没有所需权限。 |
QSharedMemory::InvalidSize
|
2
|
创建操作失败,因为请求大小无效。 |
QSharedMemory::KeyError
|
3
|
操作失败,因为键无效。 |
QSharedMemory::AlreadyExists
|
4
|
A create () 操作失败,因为具有指定键的共享内存段已存在。 |
QSharedMemory::NotFound
|
5
|
An attach () 失败,因为找不到具有指定键的共享内存段。 |
QSharedMemory::LockError
|
6
|
试图 lock () 共享内存段失败,因为 create () 或 attach () 失败和返回 false,或因为发生系统错误在 QSystemSemaphore::acquire (). |
QSharedMemory::OutOfResources
|
7
|
A create () 操作失败,因为没有足够内存可用于填充请求。 |
QSharedMemory::UnknownError
|
8
|
发生其它事情且很糟糕。 |
构造共享内存对象采用给定 parent and with the legacy key set to key 。因为有设置键,其 create () 和 attach () 函数可以被调用。
另请参阅 setKey (), create (),和 attach ().
此函数重载 QSharedMemory()。
构造共享内存对象采用给定 parent 。由于构造函数未设置共享内存对象的键,因此,共享内存对象没有附加底层共享内存段。必须设置键采用 setNativeKey () 先于 create () 或 attach () 可以使用。
另请参阅 setNativeKey ().
这是重载函数。
构造共享内存对象采用给定 parent 并将其键设为 key 。因为有设置键,其 create () 和 attach () 函数可以被调用。
另请参阅 setNativeKey (), create (),和 attach ().
[virtual noexcept]
QSharedMemory::
~QSharedMemory
()
析构函数清零键,强制共享内存对象 detach 从其底层共享内存段。若此共享内存对象是连接到共享内存段的最后一个, detach () 操作会销毁共享内存段。
另请参阅 detach () 和 isAttached ().
试图将进程附加到共享内存段,由传递给构造函数的键标识或调用
setNativeKey
()。访问
mode
is
ReadWrite
默认情况下。它还可以为
ReadOnly
。返回
true
若附加操作成功。若返回 false,调用
error
() 以确定发生何种错误。在附加共享内存段后,可以获得共享内存的指针通过调用
data
().
另请参阅 isAttached (), detach (),和 create ().
Returns a const pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. The value returned by this function will not change until a detach happens, so it is safe to store this pointer.
If the memory operations are not atomic, you may lock the shared memory with lock () before reading from or writing, but remember to release the lock with unlock () 在完成后。
创建共享内存段
size
bytes with the key passed to the constructor or set with
setNativeKey
(),然后附加到新共享内存段采用给定访问
mode
并返回
true
。若由键标识的共享内存段已存在,不履行附加操作和
false
被返回。当返回值为
false
,调用
error
() 以确定发生何种错误。
另请参阅 error ().
Returns a pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. The value returned by this function will not change until a detach happens, so it is safe to store this pointer.
If the memory operations are not atomic, you may lock the shared memory with lock () before reading from or writing, but remember to release the lock with unlock () 在完成后。
另请参阅 attach ().
此函数重载 data()。
从共享内存段分离进程。若这是附加到共享存储段的最后进程,则共享存储段由系统释放 (即:销毁内容)。函数返回
true
若它分离了共享内存段。若它返回
false
,通常意味着段未被附加或被另一进程锁定。
另请参阅 attach () 和 isAttached ().
返回指示是否发生错误的值,且若如此,指示错误是什么。
另请参阅 errorString ().
返回最后发生错误的文本描述。若 error () 返回 错误值 ,调用此函数以获取描述错误的文本字符串。
另请参阅 error ().
返回
true
若此进程被附加到共享内存段。
Returns the legacy key assigned with setKey () 到此共享内存,或 null 键若尚未赋值键,或段使用的是 nativeKey ()。键是 Qt 应用程序用来标识共享内存段的标识符。
可以找到由操作系统使用的本机、特定平台键,通过调用 nativeKey ().
另请参阅 setKey () 和 setNativeKey ().
这是锁供此进程访问的共享内存段的信号量,并返回
true
。若另一进程已锁定段,此函数阻塞直到锁被释放为止。然后,获取锁并返回
true
。若此函数返回
false
,意味着已忽略的 false 返回来自
create
() 或
attach
(),已设置键采用
setNativeKey
() 或
QSystemSemaphore::acquire
() 失败由于未知系统错误。
另请参阅 unlock (), data (),和 QSystemSemaphore::acquire ().
[since 6.6]
QNativeIpcKey
QSharedMemory::
nativeIpcKey
() const
Returns the key type for this shared memory object. The key type complements the nativeKey () as the identifier used by the operating system to identify the shared memory segment.
You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications. See Native IPC Keys 了解更多信息。
该函数在 Qt 6.6 引入。
另请参阅 nativeKey () 和 setNativeKey ().
返回用于此共享内存对象的本机、特定平台键。本机键是操作系统用于标识共享内存段的标识符。
You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications. See Native IPC Keys 了解更多信息。
另请参阅 setNativeKey () 和 nativeIpcKey ().
这是重载函数。
Sets the legacy key 对于此共享内存对象。若 key is the same as the current key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach 从它在设置新键之前。此函数不履行 attach ().
可以调用 key () to retrieve the legacy key. This function is mostly the same as:
shm.setNativeKey(QSharedMemory::legacyNativeKey(key));
except that it enables obtaining the legacy key using key ().
另请参阅 key (), nativeKey (),和 isAttached ().
[since 6.6]
void
QSharedMemory::
setNativeKey
(const
QNativeIpcKey
&
key
)
设置本机,特定平台, key 对于此共享内存对象。若 key is the same as the current native key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach 从它在设置新键之前。此函数不履行 attach ().
This function is useful if the native key was shared from another process. See Native IPC Keys 了解更多信息。
Portable native keys can be obtained using platformSafeKey().
可以调用 nativeKey () to retrieve the native key.
该函数在 Qt 6.6 引入。
另请参阅 nativeKey (), nativeIpcKey (),和 isAttached ().
设置本机,特定平台, key for this shared memory object of type type (the type parameter has been available since Qt 6.6). If key is the same as the current native key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach 从它在设置新键之前。此函数不履行 attach ().
This function is useful if the native key was shared from another process, though the application must take care to ensure the key type matches what the other process expects. See Native IPC Keys 了解更多信息。
Portable native keys can be obtained using platformSafeKey().
可以调用 nativeKey () to retrieve the native key.
另请参阅 nativeKey (), nativeIpcKey (),和 isAttached ().
返回共享内存段的附加大小。若未附加共享内存段,返回 0。
注意: 段大小可能大于请求大小,被传递给 create ().
释放共享内存段锁并返回
true
,若锁目前由此进程保持。若段未被锁定,或锁由另一进程所保持,则什么也不会发生并返回 false。
另请参阅 lock ().