The QSharedMemory class provides access to a shared memory segment.More...
Header: | #include <QSharedMemory> |
qmake: | QT += core |
Since: | Qt 4.4 |
Inherits: | QObject |
This class was introduced in Qt 4.4.
enum | AccessMode { ReadOnly, ReadWrite } |
enum | SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, AlreadyExists, …, UnknownError } |
QSharedMemory(const QString &key, QObject *parent = nullptr) | |
QSharedMemory(QObject *parent = nullptr) | |
virtual | ~QSharedMemory() |
bool | attach(QSharedMemory::AccessModemode = ReadWrite) |
const void * | constData() const |
bool | create(intsize, QSharedMemory::AccessModemode = ReadWrite) |
void * | data() |
const void * | data() const |
bool | detach() |
QSharedMemory::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() |
QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.
When using this class, be aware of the following platform differences:
Remember to lock the shared memory withlock() before reading from or writing to the shared memory, and remember to release the lock withunlock() after you are done.
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.
Warning:QSharedMemory changes the key in a Qt-specific way, unless otherwise specified. Interoperation with non-Qt applications is achieved by first creating a default shared memory with QSharedMemory() and then setting a native key withsetNativeKey(). When using native keys, shared memory is not protected against multiple accesses on it (for example, unable tolock()) and a user-defined mechanism should be used to achieve such protection.
Constant | Value | Description |
---|---|---|
QSharedMemory::ReadOnly | 0 | The shared memory segment is read-only. Writing to the shared memory segment is not allowed. An attempt to write to a shared memory segment created with ReadOnly causes the program to abort. |
QSharedMemory::ReadWrite | 1 | Reading and writing the shared memory segment are both allowed. |
Constant | Value | Description |
---|---|---|
QSharedMemory::NoError | 0 | No error occurred. |
QSharedMemory::PermissionDenied | 1 | The operation failed because the caller didn't have the required permissions. |
QSharedMemory::InvalidSize | 2 | A create operation failed because the requested size was invalid. |
QSharedMemory::KeyError | 3 | The operation failed because of an invalid key. |
QSharedMemory::AlreadyExists | 4 | Acreate() operation failed because a shared memory segment with the specified key already existed. |
QSharedMemory::NotFound | 5 | Anattach() failed because a shared memory segment with the specified key could not be found. |
QSharedMemory::LockError | 6 | The attempt tolock() the shared memory segment failed becausecreate() orattach() failed and returned false, or because a system error occurred inQSystemSemaphore::acquire(). |
QSharedMemory::OutOfResources | 7 | Acreate() operation failed because there was not enough memory available to fill the request. |
QSharedMemory::UnknownError | 8 | Something else happened and it was bad. |
Constructs a shared memory object with the givenparent and with its key set tokey. Because its key is set, itscreate() andattach() functions can be called.
See alsosetKey(),create(), andattach().
This function overloads QSharedMemory().
Constructs a shared memory object with the givenparent. 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 withsetKey() orsetNativeKey() beforecreate() orattach() can be used.
See alsosetKey().
[virtual]
QSharedMemory::~QSharedMemory()The destructor clears the key, which forces the shared memory object todetach from its underlying shared memory segment. If this shared memory object is the last one connected to the shared memory segment, thedetach() operation destroys the shared memory segment.
See alsodetach() andisAttached().
Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call tosetKey() orsetNativeKey(). The accessmode isReadWrite by default. It can also beReadOnly. Returnstrue
if the attach operation is successful. If false is returned, callerror() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by callingdata().
See alsoisAttached(),detach(), andcreate().
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 withlock() before reading from or writing to the shared memory, and remember to release the lock withunlock() after you are done.
Creates a shared memory segment ofsize bytes with the key passed to the constructor, set withsetKey() or set withsetNativeKey(), then attaches to the new shared memory segment with the given accessmode and returnstrue
. If a shared memory segment identified by the key already exists, the attach operation is not performed andfalse
is returned. When the return value isfalse
, callerror() to determine which error occurred.
See alsoerror().
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 withlock() before reading from or writing to the shared memory, and remember to release the lock withunlock() after you are done.
See alsoattach().
This function overloads data().
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 returnstrue
if it detaches the shared memory segment. If it returnsfalse
, it usually means the segment either isn't attached, or it is locked by another process.
See alsoattach() andisAttached().
Returns a value indicating whether an error occurred, and, if so, which error it was.
See alsoerrorString().
Returns a text description of the last error that occurred. Iferror() returns anerror value, call this function to get a text string that describes the error.
See alsoerror().
Returnstrue
if this process is attached to the shared memory segment.
Returns the key assigned withsetKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using anativeKey(). 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 callingnativeKey().
See alsosetKey() andsetNativeKey().
This is a semaphore that locks the shared memory segment for access by this process and returnstrue
. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returnstrue
. If this function returnsfalse
, it means that you have ignored a false return fromcreate() orattach(), that you have set the key withsetNativeKey() or thatQSystemSemaphore::acquire() failed due to an unknown system error.
See alsounlock(),data(), andQSystemSemaphore::acquire().
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.
See alsosetKey() andsetNativeKey().
Sets the platform independentkey for this shared memory object. Ifkey is the same as the current key, the function returns without doing anything.
You can callkey() to retrieve the platform independent key. Internally,QSharedMemory converts this key into a platform specific key. If you instead callnativeKey(), you will get the platform specific, converted key.
If the shared memory object is attached to an underlying shared memory segment, it willdetach from it before setting the new key. This function does not do anattach().
See alsokey(),nativeKey(), andisAttached().
Sets the native, platform specific,key for this shared memory object. Ifkey 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 callsetKey() instead.
You can callnativeKey() to retrieve the native key. If a native key has been assigned, callingkey() will return a null string.
If the shared memory object is attached to an underlying shared memory segment, it willdetach from it before setting the new key. This function does not do anattach().
The application will not be portable if you set a native key.
This function was introduced in Qt 4.8.
See alsonativeKey(),key(), andisAttached().
Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.
Note:The size of the segment may be larger than the requested size that was passed tocreate().
Releases the lock on the shared memory segment and returnstrue
, 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.
See alsolock().
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Qt Group includes The Qt Company Oy and its global subsidiaries and affiliates.