QSharedMemory Class

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.

Public Types

enum AccessMode { ReadOnly, ReadWrite }
enum SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, AlreadyExists, …, UnknownError }

Detailed Description

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:

  • Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Windows kernel releases the shared memory segment automatically.
  • Unix: QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.
  • HP-UX: Only one attach to a shared memory segment is allowed per process. This means that QSharedMemory should not be used across multiple threads in the same process in HP-UX.

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.

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 with setNativeKey(). When using native keys, shared memory is not protected against multiple accesses on it (for example, unable to lock()) and a user-defined mechanism should be used to achieve such protection.

Member Type Documentation

enum QSharedMemory::AccessMode

ConstantValueDescription
QSharedMemory::ReadOnly0The 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::ReadWrite1Reading and writing the shared memory segment are both allowed.

enum QSharedMemory::SharedMemoryError

ConstantValueDescription
QSharedMemory::NoError0No error occurred.
QSharedMemory::PermissionDenied1The operation failed because the caller didn't have the required permissions.
QSharedMemory::InvalidSize2A create operation failed because the requested size was invalid.
QSharedMemory::KeyError3The operation failed because of an invalid key.
QSharedMemory::AlreadyExists4A create() operation failed because a shared memory segment with the specified key already existed.
QSharedMemory::NotFound5An attach() failed because a shared memory segment with the specified key could not be found.
QSharedMemory::LockError6The attempt to lock() the shared memory segment failed because create() or attach() failed and returned false, or because a system error occurred in QSystemSemaphore::acquire().
QSharedMemory::OutOfResources7A create() operation failed because there was not enough memory available to fill the request.
QSharedMemory::UnknownError8Something else happened and it was bad.