"QT" QT interprocess communication (shared memory) "Go"

Source: Internet
Author: User
Tags int size

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
      • Description
      • Realize
    • Read from memory
      • Description
      • Realize

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:

    1. Detects whether the process is connected to a shared memory segment and, if so, separates the process from the shared memory segment.
    2. Get a new shared memory segment from the system's large enough memory.
    3. 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.
    4. 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:

    1. Bind the process to the shared memory segment created by process a
    2. Locks the shared memory segment, copies the data to the buffer, and then writes to the Qimage.
    3. 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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.