Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QThread - readers and writer



  • Hi,

    I would like to learn how to use QThread and QSemaphore. I started with mission, where I have 2 readers and 1 writer. There is sharedVariable, which I increment in writer Object. I would like to read this variable's value in reader Objects. I debuq information about threadID and variable value.

    I would like to achieve ( for example thread 1 with reader is 0x1 and thread 2 with other reader is x02 ):
    0x1 1
    0x2 1
    0x1 2
    0x2 2
    0x1 3
    0x2 3
    0x1 4
    0x2 4
    0x1 5
    0x2 5

    Now I get ( my debuq - this is only part ):
    0x1e28 38
    0x1e28 38
    0x1e28 38
    0x1e28 38
    0x1e28 46
    0x1e28 46
    0x1e28 46

    So for example I don't get on debuq value 39, 40, 41, 42.

    And...
    I see each value which is presented only in one thread like this:
    0x1e28 215
    0x1e28 215
    0x1e28 215
    0x1e28 218
    0x1e28 218
    0x1e28 218

    215 is only in 0x1e28 debuq and 218 is only in 0x1e28 thread.

    My codes:
    WriterClass

    #include "writerclass.h"
    #include <QDebug>
    
    WriterClass::WriterClass(int &sharedVariable, QSemaphore *sharedSemaphore): sharedVar(sharedVariable), sharedSem(sharedSemaphore)
    {}
    
    void WriterClass::process()
    {
        for(int i=0;i<1000;i++)
        {
             sharedSem->acquire(1);
             sharedVar++;
             sharedSem->release(1);
        }
        emit finished();
    }
    

    Reader Class:

    #include "readerclass.h"
    #include <QDebug>
    
    ReaderClass::ReaderClass(int &sharedVariable, QSemaphore *sharedSemaphore): sharedVar(sharedVariable), sharedSem(sharedSemaphore)
    {}
    
    void ReaderClass::process()
    {
        while(1)
        {
            sharedSem->acquire(1);
            qInfo()<<QThread::currentThreadId()<<sharedVar;
            if(sharedVar == 1000)
            {
                break;
            }
            sharedSem->release(1);
        }
        emit finished();
    }
    

    and mainWindow ( number is int and this is class variable, which started with value 0 ):

        sem = new QSemaphore(1);
        ReaderClass *reader = new ReaderClass(number,sem);
        ReaderClass *reader2 = new ReaderClass(number,sem);
        WriterClass *writer1 = new WriterClass(number,sem);
    
        QThread* thread = new QThread;
        QThread* thread2 = new QThread;
        QThread* thread3 = new QThread;
        reader->moveToThread(thread);
        writer1->moveToThread(thread2);
        reader2->moveToThread(thread3);
    
        connect(thread, SIGNAL(started()), reader, SLOT(process()));
        connect(reader, SIGNAL(finished()), thread, SLOT(quit()));
        connect(reader, SIGNAL(finished()), reader, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    
        connect(thread2, SIGNAL(started()), writer1, SLOT(process()));
        connect(writer1, SIGNAL(finished()), thread2, SLOT(quit()));
        connect(writer1, SIGNAL(finished()), writer1, SLOT(deleteLater()));
        connect(thread2, SIGNAL(finished()), thread2, SLOT(deleteLater()));
    
        connect(thread3, SIGNAL(started()), reader2, SLOT(process()));
        connect(reader2, SIGNAL(finished()), thread3, SLOT(quit()));
        connect(reader2, SIGNAL(finished()), reader2, SLOT(deleteLater()));
        connect(thread3, SIGNAL(finished()), thread3, SLOT(deleteLater()));
    
        thread->start();
        thread2->start();
        thread3->start();
    

  • Lifetime Qt Champion

    Why do you think that after the reader executes sharedSem->release(1) a thread context will happen? Same goes for the writer thread.


Log in to reply