Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How communicate two threads from QSharedMemory

How communicate two threads from QSharedMemory

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sayeh_sh
    wrote on last edited by
    #1

    please help me:
    i want to communicate 2 threads :
    thread1 --> write in a segment of QSharedMemory
    thread2 --> read from this segment
    I have do this work but i dosen't have open the segment from another class!!!!!!

    class 1:
    @#include "mythread.h"

    #include <QtCore>
    #include <QDebug>

    MyThread::MyThread()
    {

    }

    void MyThread::run()
    {
    qDebug() << this->name <<"Running ...";
    sleep(3);
    QSharedMemory shared("memory");
    //shared.setKey("memory");
    shared.attach(QSharedMemory::ReadWrite);
    shared.lock();
    char to = (char)shared.data();
    qDebug() << "the content of segment is : " << to;
    shared.unlock();

    shared.detach();
    qDebug() << "segment is deleted";
    

    }
    @

    class 2:
    @#include "thread.h"

    #include <QtCore>
    #include <QDebug>

    Thread::Thread()
    {
    }

    void Thread::run()
    {
    qDebug() << this->name <<"Runned thread2 MSG!!!";

    QSharedMemory shared("memory");
    if(!shared.create(1024,QSharedMemory::ReadWrite))
    {
        qDebug() << "segment not be create!!!";
    }
    qDebug() << "segment memoire crée";
    shared.lock();
    char *to = (char*)shared.data();
    char *text = "SND_SV1_LCH_DATA";
    memcpy(to, text, strlen(text)+1);
    shared.unlock();
    qDebug()<<"thread2 terminated";
    

    }@

    main :
    @
    #include <QCoreApplication>
    #include "mythread.h"
    #include "thread.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    MyThread mThread1;
    mThread1.name = "mThread1";
    
    
    Thread mThread2;
    mThread2.name = "mThread2";
    
    QMutex mutex;
    mutex.lock();
    mThread2.start();
    mutex.unlock();
    
    sleep(5);
    
    mutex.lock();
    mThread1.start();
    mutex.unlock();
    
    
    //mThread1.Stop = false;
    
    
    return a.exec(&#41;;
    

    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You are creating the threads in the wrong order. You start by creating the thread that attaches to the QSharedMemory and then the thread that creates it. It's the other way around.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sayeh_sh
        wrote on last edited by
        #3

        thnx for your response
        i inversed the order of threads but it's still wrong.
        please you can help me!!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I didn't pay attention to one detail.

          Short version: in your run code, you are creating the QSharedMemory, writing to it and then, end of the function. This means that your QSharedMemory goes out of scope thus it's destroyed even before you start your second thread.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sayeh_sh
            wrote on last edited by
            #5

            so i do it in this case!!
            and it can possible to give me some example illustrate a communication between 3 process 1 parent and 2 childs using QProcess and QSharedMemory!!!!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Less exclamation points please.

              So this is school work...

              Search Qt's documentation for the shared memory documentation and example.

              Study the example carefully, it contains everything you need to get going.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved