Copyright notice: If no source note, techie bright blog article are original. Reprint please indicate the title and address of this article in the form of link:
This article is entitled: Qsharedmemory shared Memory for interprocess communication (IPC) and forbidden programs multiple opening this article address: http://techieliang.com/2017/12/685/
Article Directory
- 1. Introduction
- 2. Example
- 3. No more open programs
1. Introduction
Very simple library, look directly at the Help document: http://doc.qt.io/qt-5/qsharedmemory.html
Main function: Set Key,create to the system request to establish a memory space, attach current process and memory binding, detach unbind, Lock/unlock sync lock, Data/constdata get memory pointer
Creator process: Setkey,create,attach,lock,data, Operation Data,unlock, no time detach
Visitor: Setkey,attach,lock,data, Operation Data,unlock, not when detach?? No need for create.
- As the creator should be sure that no one else will be able to unbind
- Qsharedmemory destruction is also automatically detach
- One memory space if 0 attach will be destroyed, the data will be gone.
- Read and write operation remember Lock, and be careful not to forget the unlock
- Without the Create key, the call to attach will return false, note that this sentence in the Prohibition program more open useful
Size gets the shared memory, error/errorstring is the error message, and isattached determines whether the current process is bound to memory.
2. Example
Steal a lazy, write together:
- #include <QCoreApplication>
- #include <QSharedMemory>
- #include <QDebug>
- int main(int argc, char *argv[]) {
- Qcoreapplication a(argc, argv);
- //Create a key at the same time, or you can setkey
- Qsharedmemory sm("test_shared");
- //If it is the first use you must first create a
- //The remaining processes do not need to create direct attach
- if(!sm.Create(1024x768))
- qdebug()<<"Create Error";
- Sm.Attach();//bind memory
- //attach returns BOOL, which can be judged without the following method
- if(!sm.isattached())
- qdebug()<<"Attach error";
- Sm.Lock();
- int *memdata = static_cast<int*>( sm.Data() ) ;
- *memdata = 1024x768;
- Sm.unlock();
- //So lazy! Want to test a multi-process to build a project under the cuff.
- Qsharedmemory testsm("test_shared");
- Testsm. Attach();
- int *testdata = static_cast<int*>( testsm. Data() );
- qdebug()<<*testdata;
- //separates the current process from memory, and automatically calls when the destructor is made
- Sm.Detach();
- return A.exec();
- }
TESTSM will call detach when it is destructor, can Sm.detach (); Put in Qsharedmemory testsm ("test_shared"); You'll see a mistake.
3. No more open programs
- #include <QCoreApplication>
- #include <QSharedMemory>
- int main(int argc, char *argv[]) {
- Qcoreapplication a(argc, argv);
- //Create a key at the same time, or you can setkey
- Qsharedmemory sm("test_shared");
- if(sm.Attach())
- return 0;
- Sm.Create(1);
- MainWindow W;
- W.show();
- return A.exec();
- }
Very simple principle, as long as there is an open success that will create a 1-size space, follow-up program can be attach and then return ...
Reprint please indicate the title and address of this article in the form of a link: techie bright blog»qsharedmemory shared memory for interprocess communication (IPC) and Prohibition programs open more
Qsharedmemory shared memory for inter-process communication (IPC) and prohibit programs to open