—————————————————— Write part ——————————————————
(This program is based on the console program)
First use shared memory to summon:
#include <QSharedMemory>
Then declare the Qsharedmemory class. and named Smem.
Qsharedmemory Smem;
Shared memory cannot be found because it does not have an access key. We'll set him up. access keys
QString key,sharedstring;
Set access keys
Qdebug () << "Please input the shared memory key:";
CIN >> Key;
Then declare the buffer and the data flow. Buffers are used to transform and save data (to reference both libraries before use)
Qbuffer buffer; Buffer
Qdatastream QDSM (&buffer); Data flow
Determines whether to attach to shared memory after the access key is entered. If yes, then detach
if (smem.isattached ()) Smem.detach (); Determine if you are connected to a shared memory block if it is, split it up first.
Let the user enter the shared data
Qdebug () << "Please input the string to share:";
Cin >> sharedstring;
Open buffer in a readable and writable manner
Buffer.open (Qbuffer::readwrite); Set Read mode
Enter a shared string into the data stream
QDSM << sharedstring; Enter a shared string to the data stream
The buffer is affected after the input to the data stream. The buffer will get the size of the data
int size = Buffer.size (); Get byte size
Allow shared memory to create a memory space that is the size of the buffer data. and detect if the creation was successful
if (!smem.create (size))//Detect if Shared memory segment is created successfully
{
Qdebug () << "Could not create sharing memory";
return A.exec ();
}
If the creation succeeds. In order to let the program do read and write operations, do not let other programs affect. We'll lock up the shared memory first.
Smem.lock (); Lock shared Memory
We first get pointers to the data that was created after the shared memory, and for the strings to be shared.
char * to = (char *) smem.data (); Shared-memory data
const char * from = Buffer.data (). data (); Data that is shared memory
Use the memory copy function (memcpy) to copy the data to be shared into the shared data space. The size of the copied data is the middle of the two. Which is small is the size of the data to be shared (no confirmation)
memcpy (To,from,qmin (Smem.size (), size)); Copy the memory to be shared to the shared memory
Unlocks the shared memory space. Enable it to be accessed by other programs
Smem.unlock (); Unlock shared memory
—————————————————— Read Part ——————————————————
Still do not mistakenly refer to the previous part of the header file ...
Declaring shared memory as usual
Qsharedmemory Sharemem;
Declaring the shared memory key and the read-out string
QString key,readstring;
declaring buffers
Qbuffer buffer;
Declaring a data flow
Qdatastream out (&buffer);
Let the user enter a key for shared memory
Qdebug () << "Please input the shared memory key:";
CIN >> Key;
Sets the access key for shared memory. Enables it to find the specified shared memory
Sharemem.setkey (key);
This memory is associated after the specified shared memory is found (as can be understood here: go to someone's home for something)
Sharemem.attach ();
Lock the shared memory. (This can be understood as: Close the door, do not allow others to come in)
Sharemem.lock ();
The size of data and data obtained by using buffers for shared memory associations
Buffer.setdata (char *) sharemem.constdata (), sharemem.size ());
Open buffer for access
Buffer.open (qbuffer::readonly);
Use a data stream to get the shared memory data from the buffer and then output it to a string
Out >> ReadString;
Unlock Shared memory space (this can be understood as: open the door and allow others to come in)
Sharemem.unlock ();
Separate from the shared memory space (this can be understood as: I'm gone, I'm out of the house)
Sharemem.detach ();
Qt Shared Memory (qsharedmemory)