QSharedMemory Class
The QSharedMemory class provides access to a shared memory segment. 更多...
头文件: | #include <QSharedMemory> |
qmake: | QT += core |
开始支持版本: | Qt 4.4 |
基类: | QObject |
公有类型
enum | AccessMode { ReadOnly, ReadWrite } |
enum | SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, ..., UnknownError } |
公有函数
QSharedMemory(const QString &key, QObject *parent = Q_NULLPTR) | |
QSharedMemory(QObject *parent = Q_NULLPTR) | |
~QSharedMemory() | |
bool | attach(AccessMode mode = ReadWrite) |
const void * | constData() const |
bool | create(int size, AccessMode mode = ReadWrite) |
void * | data() |
const void * | data() const |
bool | detach() |
SharedMemoryError | error() const |
QString | errorString() const |
bool | isAttached() const |
QString | key() const |
bool | lock() |
QString | nativeKey() const |
void | setKey(const QString &key) |
void | setNativeKey(const QString &key) |
int | size() const |
bool | unlock() |
- 32 个公有函数继承自 QObject
其他继承的成员
详细描述
QSharedMemory 访问共享内存.
QSharedMemory 支持多个线程和进程访问共享内存. 对于单一线程或进程, 它还支持以独占方式访问内存.
使用QSharedMemory时, 你需要注意平台间差异:
- Windows: QSharedMemory 不 "拥有" 共享内存. 当所有附加到特定共享内存的 QSharedMemory 实例的线程或进程退出时, Windows内核会自动释放共享内存.
- Unix: QSharedMemory "拥有" 共享内存. 最后一个附加到特定共享内存的 QSharedMemory 实例的线程或进程分离时, Unix内核会释放共享内存. 但是, 如果最后一个线程或进程在没有运行 QSharedMemory 析构函数的情况下崩溃, 则共享内存在崩溃后仍然存在.
- HP-UX: 每个进程只允许连接一个共享内存. 这意味着在HP-UX中, 不应在同一个进程中的多个线程之间使用 QSharedMemory.
在读取或写入共享内存前, 必须调用 lock() 锁定共享内存, 完成后调用 unlock() 释放锁定.
QSharedMemory 的最后一个实例与共享内存分离时, QSharedMemory 会自动销毁共享内存, 并且不会保留对内存的引用.
警告: 除非另有指定, 否则QSharedMemory 会以特定于Qt的方式更改key. 若与非Qt的应用程序通信, 首先使用 QSharedMemory() 创建默认共享内存, 然后调用 setNativeKey()设置原生key. 使用原生key时, 共享内存不会受到多次访问保护(例如, 调用 lock()无效), 你应该自定义保护机制.
成员类型
enum QSharedMemory::AccessMode
Constant | Value | Description |
---|---|---|
QSharedMemory::ReadOnly | 0 | 共享内存是只读的. 不允许写操作. 对共享内存执行写操作会造成程序中止. |
QSharedMemory::ReadWrite | 1 | 共享内存允许读写. |
enum QSharedMemory::SharedMemoryError
Constant | Value | Description |
---|---|---|
QSharedMemory::NoError | 0 | 无错误发生. |
QSharedMemory::PermissionDenied | 1 | 调用者没有要求权限. |
QSharedMemory::InvalidSize | 2 | 创建操作失败, 请求的大小无效. |
QSharedMemory::KeyError | 3 | 无效的key. |
QSharedMemory::AlreadyExists | 4 | create() 操作失败, key对应的共享内存已存在. |
QSharedMemory::NotFound | 5 | attach() 失败, key对应的共享内存未找到. |
QSharedMemory::LockError | 6 | 对共享内存执行 to lock() 失败, 可能的原因有 create() 或 attach() 返回false, 或者调用 QSystemSemaphore::acquire()发生错误. |
QSharedMemory::OutOfResources | 7 | A create() 操作失败, 没有足够的内存. |
QSharedMemory::UnknownError | 8 | 未知错误. |
成员函数
QSharedMemory::QSharedMemory(const QString &key, QObject *parent = Q_NULLPTR)
Constructs a shared memory object with the given parent and with its key set to key. Because its key is set, its create() and attach() functions can be called.
参见 setKey(), create(), and attach().
QSharedMemory::QSharedMemory(QObject *parent = Q_NULLPTR)
This function overloads QSharedMemory().
Constructs a shared memory object with the given parent. The shared memory object's key is not set by the constructor, so the shared memory object does not have an underlying shared memory segment attached. The key must be set with setKey() or setNativeKey() before create() or attach() can be used.
参见 setKey().
QSharedMemory::~QSharedMemory()
The destructor clears the key, which forces the shared memory object to detach from its underlying shared memory segment. If this shared memory object is the last one connected to the shared memory segment, the detach() operation destroys the shared memory segment.
参见 detach() and isAttached().
bool QSharedMemory::attach(AccessMode mode = ReadWrite)
Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setKey() or setNativeKey(). The access mode is ReadWrite by default. It can also be ReadOnly. Returns true
if the attach operation is successful. If false is returned, call error() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data().
参见 isAttached(), detach(), and create().
const void *QSharedMemory::constData() const
Returns a const pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
bool QSharedMemory::create(int size, AccessMode mode = ReadWrite)
Creates a shared memory segment of size bytes with the key passed to the constructor, set with setKey() or set with setNativeKey(), then attaches to the new shared memory segment with the given access mode and returns true
. If a shared memory segment identified by the key already exists, the attach operation is not performed and false
is returned. When the return value is false
, call error() to determine which error occurred.
参见 error().
void *QSharedMemory::data()
Returns a pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
参见 attach().
const void *QSharedMemory::data() const
This function overloads data().
bool QSharedMemory::detach()
Detaches the process from the shared memory segment. If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true
if it detaches the shared memory segment. If it returns false
, it usually means the segment either isn't attached, or it is locked by another process.
参见 attach() and isAttached().
SharedMemoryError QSharedMemory::error() const
Returns a value indicating whether an error occurred, and, if so, which error it was.
参见 errorString().
QString QSharedMemory::errorString() const
Returns a text description of the last error that occurred. If error() returns an error value, call this function to get a text string that describes the error.
参见 error().
bool QSharedMemory::isAttached() const
Returns true
if this process is attached to the shared memory segment.
QString QSharedMemory::key() const
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey(). The key is the identifier used by Qt applications to identify the shared memory segment.
You can find the native, platform specific, key used by the operating system by calling nativeKey().
参见 setKey() and setNativeKey().
bool QSharedMemory::lock()
This is a semaphore that locks the shared memory segment for access by this process and returns true
. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true
. If this function returns false
, it means that you have ignored a false return from create() or attach(), that you have set the key with setNativeKey() or that QSystemSemaphore::acquire() failed due to an unknown system error.
参见 unlock(), data(), and QSystemSemaphore::acquire().
QString QSharedMemory::nativeKey() const
Returns the native, platform specific, key for this shared memory object. The native key is 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.
This function was introduced in Qt 4.8.
参见 setKey() and setNativeKey().
void QSharedMemory::setKey(const QString &key)
Sets the platform independent key for this shared memory object. If key is the same as the current key, the function returns without doing anything.
You can call key() to retrieve the platform independent key. Internally, QSharedMemory converts this key into a platform specific key. If you instead call nativeKey(), you will get the platform specific, converted key.
If the shared memory object is attached to an underlying shared memory segment, it will detach from it before setting the new key. This function does not do an attach().
参见 key(), nativeKey(), and isAttached().
void QSharedMemory::setNativeKey(const QString &key)
Sets the native, platform specific, key for this shared memory object. If key is the same as the current native key, the function returns without doing anything. If all you want is to assign a key to a segment, you should call setKey() instead.
You can call nativeKey() to retrieve the native key. If a native key has been assigned, calling key() will return a null string.
If the shared memory object is attached to an underlying shared memory segment, it will detach from it before setting the new key. This function does not do an attach().
The application will not be portable if you set a native key.
This function was introduced in Qt 4.8.
参见 nativeKey(), key(), and isAttached().
int QSharedMemory::size() const
Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.
bool QSharedMemory::unlock()
Releases the lock on the shared memory segment and returns true
, if the lock is currently held by this process. If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.
参见 lock().