The QSharedMemory class provides access to a shared memory segment. 更多...
继承 QObject .
The QSharedMemory class provides access to a shared memory segment.
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:
记住锁定共享内存采用 lock () before reading from or writing to the shared memory, and remember to release the lock with unlock () after you are done.
不像 QtSharedMemory , 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. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.
警告: 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 (e.g. unable to lock ()) and a user-defined mechanism should be used to achieve a such protection.
| 常量 | 值 | 描述 |
|---|---|---|
| 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. |
| 常量 | 值 | 描述 |
|---|---|---|
| QSharedMemory.NoError | 0 | 没有发生错误。 |
| 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 | A create () operation failed because a shared memory segment with the specified key already existed. |
| QSharedMemory.NotFound | 5 | An attach () failed because a shared memory segment with the specified key could not be found. |
| QSharedMemory.LockError | 6 | 试图 lock () the shared memory segment failed because create () 或 attach () failed and returned false, or because a system error occurred in QSystemSemaphore.acquire (). |
| QSharedMemory.OutOfResources | 7 | A create () operation failed because there was not enough memory available to fill the request. |
| QSharedMemory.UnknownError | 8 | 发生其它事情且很糟糕。 |
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造共享内存对象采用给定 parent 并将其键设为 key 。因为有设置键,其 create () 和 attach () functions can be called.
另请参阅 setKey (), create (),和 attach ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
此函数重载 QSharedMemory ().
构造共享内存对象采用给定 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 () 或 setNativeKey () 之后 create () 或 attach () 可以使用。
另请参阅 setKey ().
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 () 或 setNativeKey ()。 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 (),和 create ().
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.
创建共享内存段 size bytes with the key passed to the constructor, set with setKey () 或设置采用 setNativeKey (), then attaches to the new shared memory segment with the given access mode 并返回 true . If a shared memory segment identified by the key already exists, the attach operation is not performed and false 被返回。当返回值为 false ,调用 error () to determine which error occurred.
另请参阅 error ().
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 () 在完成后。
另请参阅 attach ().
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 ().
Returns a value indicating whether an error occurred, and, if so, which error it was.
另请参阅 errorString ().
返回最后发生错误的文本描述。若 error () 返回 错误值 ,调用 this function to get a text string that describes the error.
另请参阅 error ().
Returns true if this process is attached to the shared memory segment.
返回的键赋值采用 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 ().
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 () 或 attach (), that you have set the key with setNativeKey () 或 QSystemSemaphore.acquire () failed due to an unknown system error.
另请参阅 unlock (), data (),和 QSystemSemaphore.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.
该函数在 Qt 4.8 引入。
另请参阅 setKey () and setNativeKey ().
设置平台无关 key for this shared memory object. If key is the same as the current key, the function returns without doing anything.
可以调用 key () 到 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 (),和 isAttached ().
设置本机,特定平台, 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 () 代替。
可以调用 nativeKey () 以检索 native key. If a native key has been assigned, calling key () 将返回 null 字符串。
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.
该函数在 Qt 4.8 引入。
另请参阅 nativeKey (), key (),和 isAttached ().
Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.
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 ().