Briefly
In the previous section, we shared how to use the Windows Messaging mechanism to communicate between different processes. But there are many limitations, such as: not cross-platform, and must be two processes exist at the same time, or process a sent the message who received it?
Let's share another way of communicating across platforms-shared memory (shared).
- Briefly
- Precautions
- Load into memory
- Read from memory
Precautions
When initializing the Qsharedmemory, a unique identity key must be specified, and the key of the process must remain consistent. You can use Setkey to set up.
Load into memory description
Process A-write
It is divided into the following steps:
- Detects whether the process is connected to a shared memory segment and, if so, separates the process from the shared memory segment.
- Get a new shared memory segment from the system's large enough memory.
- Locks the shared memory segment to prevent the second dialog process from accessing and copying the picture in the buffer into the shared memory segment.
- The shared memory segment is unlocked, and then the second dialog box process is accessible.
Realize
void Dialog::loadfromfile () {if (sharedmemory.isattached ()) {Detach the process from a shared memory segmentif (!sharedmemory.detach ()) Qdebug () <<"Unable to detach from shared memory."; } QString fileName = Qfiledialog::getopenfilename (0, QString (), QString (), TR ("Images (*.png *.xpm *.jpg)"); Qimage image;if (!image.load (FileName)) {qdebug () <<"Selected file is not a image, please select another.";Return }//load data into shared memory Qbuffer buffer; Buffer.open (qbuffer::readwrite); Qdatastream out (&buffer); out << image; int size = Buffer.size (); //Create shared Memory segment if (!sharedmemory.create (size)) {Qdebug () << Sharedmemory.errorstring () << return; } sharedmemory. lock (); char *to = (char*) Sharedmemory.data (); const char *from = Buffer.data (). Data (); memcpy (To, from, Qmin (Sharedmemory.size (), size)); Sharedmemory.unlock ();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
Read the description from memory
Process B-Read
It is divided into the following steps:
- Bind the process to the shared memory segment created by process a
- Locks the shared memory segment, copies the data to the buffer, and then writes to the Qimage.
- Unlocks the shared memory segment, and then separates the process from the shared memory segment.
Realize
void MainWindow:: Loadfrommemory () {Binds shared memory to the processif (!sharedmemory. Attach ()) {Qdebug ()<<"Unable to attach to shared memory segment."; return;} //Read data from shared memory Qbuffer buffer; Qdatastream in (&buffer); Qimage image; SharedmemoryLock (); buffer. SetData ((char*) sharedmemory. Constdata (), Sharedmemory. Size ()); Buffer. Open (Qbuffer:: ReadOnly); in >> image; Sharedmemory. Unlock (); Sharedmemory. Detach (); M_plabel->setpixmap ( Qpixmap:: FromImage (image));}
原文一去丶二三里
作者博客:去作者博客空间
"QT" QT interprocess communication (shared memory) "Go"