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. QThread - readers and writer

QThread - readers and writer

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 209 Views
  • 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #1

    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();
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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