How communicate two threads from QSharedMemory



  • 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;;
    

    }@


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



  • 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!!!!


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.