QtBase
v6.3.1
|
The QSharedMemory class provides access to a shared memory segment. More...
#include <qsharedmemory.h>
Public Types | |
enum | AccessMode { ReadOnly , ReadWrite } |
enum | SharedMemoryError { NoError , PermissionDenied , InvalidSize , KeyError , AlreadyExists , NotFound , LockError , OutOfResources , UnknownError } |
Related Functions | |
(Note that these are not member functions.) | |
QSharedMemory | |
Related Functions inherited from QObject | |
template< class T > T | qobject_cast (const QObject *object) |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
The QSharedMemory class provides access to a shared memory segment.
\inmodule QtCore
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:
\list
\list
{
<application group identifier>/<custom identifier>}, as documented \l {https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW24} {here} and \l {https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups} {here}.\endlist
\endlist
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.
Definition at line 58 of file qsharedmemory.h.
\value ReadOnly 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.
\value ReadWrite Reading and writing the shared memory segment are both allowed.
Enumerator | |
---|---|
ReadOnly | |
ReadWrite |
Definition at line 69 of file qsharedmemory.h.
\value NoError No error occurred.
\value PermissionDenied The operation failed because the caller didn't have the required permissions.
\value InvalidSize A create operation failed because the requested size was invalid.
\value KeyError The operation failed because of an invalid key.
\value AlreadyExists A create() operation failed because a shared memory segment with the specified key already existed.
\value NotFound An attach() failed because a shared memory segment with the specified key could not be found.
\value LockError The 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().
\value OutOfResources A create() operation failed because there was not enough memory available to fill the request.
\value UnknownError Something else happened and it was bad.
Enumerator | |
---|---|
NoError | |
PermissionDenied | |
InvalidSize | |
KeyError | |
AlreadyExists | |
NotFound | |
LockError | |
OutOfResources | |
UnknownError |
Definition at line 75 of file qsharedmemory.h.
Definition at line 212 of file qsharedmemory.cpp.
QSharedMemory::~QSharedMemory | ( | ) |
The destructor clears the key, which forces the shared memory object to \l {detach()} {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.
Definition at line 252 of file qsharedmemory.cpp.
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 \l {QSharedMemory::} {ReadWrite} by default. It can also be \l {QSharedMemory::} {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().
Definition at line 474 of file qsharedmemory.cpp.
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.
Definition at line 553 of file qsharedmemory.cpp.
bool QSharedMemory::create | ( | qsizetype | 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 \tt true. If a shared memory segment identified by the key already exists, the attach operation is not performed and \tt false is returned. When the return value is \tt false, call error() to determine which error occurred.
Definition at line 400 of file qsharedmemory.cpp.
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.
Definition at line 538 of file qsharedmemory.cpp.
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.
Definition at line 514 of file qsharedmemory.cpp.
QSharedMemory::SharedMemoryError QSharedMemory::error | ( | ) | const |
Returns a value indicating whether an error occurred, and, if so, which error it was.
Definition at line 655 of file qsharedmemory.cpp.
QString QSharedMemory::errorString | ( | ) | const |
Returns a text description of the last error that occurred. If error() returns an \l {QSharedMemory::SharedMemoryError} {error value}, call this function to get a text string that describes the error.
Definition at line 669 of file qsharedmemory.cpp.
bool QSharedMemory::isAttached | ( | ) | const |
Returns true
if this process is attached to the shared memory segment.
Definition at line 498 of file qsharedmemory.cpp.
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().
Definition at line 366 of file qsharedmemory.cpp.
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.
Definition at line 580 of file qsharedmemory.cpp.
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.
Definition at line 384 of file qsharedmemory.cpp.
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 \l {detach()} {detach} from it before setting the new key. This function does not do an attach().
Definition at line 271 of file qsharedmemory.cpp.
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 \l {detach()} {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.
Definition at line 303 of file qsharedmemory.cpp.
qsizetype QSharedMemory::size | ( | ) | const |
Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.
Definition at line 444 of file qsharedmemory.cpp.
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.
Definition at line 605 of file qsharedmemory.cpp.
|
related |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.