Qsharedmemory shared memory for inter-process communication (IPC) and prohibit programs to open

Source: Internet
Author: User

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:

  1. #include <QCoreApplication>
  2. #include <QSharedMemory>
  3. #include <QDebug>
  4. int main(int argc, char *argv[]) {
  5. Qcoreapplication a(argc, argv);
  6. //Create a key at the same time, or you can setkey
  7. Qsharedmemory sm("test_shared");
  8. //If it is the first use you must first create a
  9. //The remaining processes do not need to create direct attach
  10. if(!sm.Create(1024x768))
  11. qdebug()<<"Create Error";
  12. Sm.Attach();//bind memory
  13. //attach returns BOOL, which can be judged without the following method
  14. if(!sm.isattached())
  15. qdebug()<<"Attach error";
  16. Sm.Lock();
  17. int *memdata = static_cast<int*>( sm.Data() ) ;
  18. *memdata = 1024x768;
  19. Sm.unlock();
  20. //So lazy! Want to test a multi-process to build a project under the cuff.
  21. Qsharedmemory testsm("test_shared");
  22. Testsm. Attach();
  23. int *testdata = static_cast<int*>( testsm. Data() );
  24. qdebug()<<*testdata;
  25. //separates the current process from memory, and automatically calls when the destructor is made
  26. Sm.Detach();
  27. return A.exec();
  28. }

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
  1. #include <QCoreApplication>
  2. #include <QSharedMemory>
  3. int main(int argc, char *argv[]) {
  4. Qcoreapplication a(argc, argv);
  5. //Create a key at the same time, or you can setkey
  6. Qsharedmemory sm("test_shared");
  7. if(sm.Attach())
  8. return 0;
  9. Sm.Create(1);
  10. MainWindow W;
  11. W.show();
  12. return A.exec();
  13. }

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

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.