QSharedMemory
The QSharedMemory class provides access to a shared memory segment.
QSharedMemory提供了使用共享内存段的方法。
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.
QSharedMemory 提供了多线程和进程使用共享内存段的方法。它也为单线程或者进程为了独占这段内存,需要用的锁。
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.
Windows: QSharedMemory不“拥有”那段共享的内存段。所有的拥有QSharedMemory的实例(依附在一个共享内存段)的线程或者进程,不管它们的QSharedMemory的实例是存在还是销毁了,windows核都会自动释放那个共享内存段。
- 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.
- Unix:QSharedMemory“拥有”该共享内存段。当最后一个拥有QSharedMemory实例(依附在一个共享内存段)的线程或者进程,通过销毁QSharedMemory实例,从该数据段解依附时,Unix核会释放该共享内存段。但是如果最后一个线程或者进程在销毁QSharedMemory之前就异常毁灭的话,共享内存段会继续存留。
- 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.
- HP-UX:每个进程只能依附一个共享内存段。这意味着QSharedMemory在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.
当对共享内存进行读写的时候,记得要先用lock()来锁住该共享内存,让读写完成之后,也要记得用unlock()释放这个锁。
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.
当最后一个QSharedMemory的实例从数据段解依之后,QSharedMemory会自动的销毁该共享内存段,并且没有引用可以找到该数据段。
浙公网安备 33010602011771号