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. Motivation for multithreading in my App

Motivation for multithreading in my App

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 6 Posters 1.9k 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.
  • A Offline
    A Offline
    Absurd
    wrote on last edited by
    #11

    @JonB Yes!
    That clears everything up.

    Thank you

    1 Reply Last reply
    1
    • A Offline
      A Offline
      Absurd
      wrote on last edited by Absurd
      #12

      Ok, this is how I transformed my code:

      In main.cpp:

      int main(int argc, char* argv[]) {
          Controller controller(argc, argv);
          return controller.runApplication();
      }
      

      And in Controller:

      Controller::Controller(int argc, char* argv[]) :  _app(argc, argv) {
          // does nothing
      }
      
      void Controller::initModule() {
          _devicesManager = new DevicesManager;
          _moduleThread = new QThread(this); // Controller will be the parent of QThread - and will be responsible for deleting it
          _devicesManager->moveToThread(_moduleThread);
      
          _moduleThread->start();
      }
      
      void Controller::initView() {
          _gui = new GUI;
      }
      
      int Controller::runView() {
          _gui->show();
          return _app.exec();
      }
      
      void Controller::connectSignals() {
          connect(_moduleThread, SIGNAL(started()), _devicesManager, SLOT(start()));
          // more to come, for now just connecting QThread's 'started' signal to  DevicesManager::start()
      }
      
      int Controller::runApplication() {
          initModule();
          initView();
          connectSignals();
          return runView();
      }
      

      For now, DeviceManager does absolutely nothing, except this:

      void DevicesManager::start() {
          qDebug() << "Starting!";
      }
      

      (that's why I didn't bother to connect QThread::finished to DeviceManager::deleteLater for now - because it holds no memory allocation)

      But I am experiencing two problems:

      1. When I run it, the GUI runs fine, but then when I close the GUI by clicking the x button of the window, I get:
        QThread: Destroyed while thread is still running
      2. I don't see the "Starting!" printed out to the console, which makes me think that start didn't really run, not to mention running on a different thread...
      J.HilkJ 1 Reply Last reply
      0
      • A Absurd

        Ok, this is how I transformed my code:

        In main.cpp:

        int main(int argc, char* argv[]) {
            Controller controller(argc, argv);
            return controller.runApplication();
        }
        

        And in Controller:

        Controller::Controller(int argc, char* argv[]) :  _app(argc, argv) {
            // does nothing
        }
        
        void Controller::initModule() {
            _devicesManager = new DevicesManager;
            _moduleThread = new QThread(this); // Controller will be the parent of QThread - and will be responsible for deleting it
            _devicesManager->moveToThread(_moduleThread);
        
            _moduleThread->start();
        }
        
        void Controller::initView() {
            _gui = new GUI;
        }
        
        int Controller::runView() {
            _gui->show();
            return _app.exec();
        }
        
        void Controller::connectSignals() {
            connect(_moduleThread, SIGNAL(started()), _devicesManager, SLOT(start()));
            // more to come, for now just connecting QThread's 'started' signal to  DevicesManager::start()
        }
        
        int Controller::runApplication() {
            initModule();
            initView();
            connectSignals();
            return runView();
        }
        

        For now, DeviceManager does absolutely nothing, except this:

        void DevicesManager::start() {
            qDebug() << "Starting!";
        }
        

        (that's why I didn't bother to connect QThread::finished to DeviceManager::deleteLater for now - because it holds no memory allocation)

        But I am experiencing two problems:

        1. When I run it, the GUI runs fine, but then when I close the GUI by clicking the x button of the window, I get:
          QThread: Destroyed while thread is still running
        2. I don't see the "Starting!" printed out to the console, which makes me think that start didn't really run, not to mention running on a different thread...
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #13

        hi, @Absurd

        you start your QThread object before you do the connection -> started signal is emitted before the connection is made -> slot gets not invoked.

        _moduleThread->start();


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        A 1 Reply Last reply
        5
        • J.HilkJ J.Hilk

          hi, @Absurd

          you start your QThread object before you do the connection -> started signal is emitted before the connection is made -> slot gets not invoked.

          _moduleThread->start();

          A Offline
          A Offline
          Absurd
          wrote on last edited by
          #14

          @J.Hilk ohh... how silly of me... :
          Thank you.

          Do you know what could be the cause for the second thing?
          QThread: Destroyed while thread is still running

          kshegunovK 1 Reply Last reply
          0
          • A Absurd

            @J.Hilk ohh... how silly of me... :
            Thank you.

            Do you know what could be the cause for the second thing?
            QThread: Destroyed while thread is still running

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #15

            @Absurd said in Motivation for multithreading in my App:

            @J.Hilk ohh... how silly of me... :
            Thank you.

            Do you know what could be the cause for the second thing?
            QThread: Destroyed while thread is still running

            Call QThread::quit and after that QThread::wait before you exit the application.

            Addendum:

            What is a good justification to use threads anyway?

            Latency. Having a slow, blocking or otherwise high latency operation you don't need or want to wait for - you need to thread it, otherwise you do not.

            Read and abide by the Qt Code of Conduct

            A 1 Reply Last reply
            2
            • kshegunovK kshegunov

              @Absurd said in Motivation for multithreading in my App:

              @J.Hilk ohh... how silly of me... :
              Thank you.

              Do you know what could be the cause for the second thing?
              QThread: Destroyed while thread is still running

              Call QThread::quit and after that QThread::wait before you exit the application.

              Addendum:

              What is a good justification to use threads anyway?

              Latency. Having a slow, blocking or otherwise high latency operation you don't need or want to wait for - you need to thread it, otherwise you do not.

              A Offline
              A Offline
              Absurd
              wrote on last edited by
              #16

              @kshegunov thanks for your reply. appreciate it.

              @kshegunov said in Motivation for multithreading in my App:

              Call QThread::quit and after that QThread::wait before you exit the application.

              I should call QThread::quit & QThread::wait from Controller's destructor, right?

              kshegunovK 1 Reply Last reply
              0
              • A Absurd

                @kshegunov thanks for your reply. appreciate it.

                @kshegunov said in Motivation for multithreading in my App:

                Call QThread::quit and after that QThread::wait before you exit the application.

                I should call QThread::quit & QThread::wait from Controller's destructor, right?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #17

                @Absurd said in Motivation for multithreading in my App:

                I should call QThread::quit & QThread::wait from Controller's destructor, right?

                Yes, that is correct. You should also free your worker objects:

                QObject::connect(_moduleThread, &QThread::finished, _devicesManager, &QObject::deleteLater);
                

                Read and abide by the Qt Code of Conduct

                A 1 Reply Last reply
                4
                • kshegunovK kshegunov

                  @Absurd said in Motivation for multithreading in my App:

                  I should call QThread::quit & QThread::wait from Controller's destructor, right?

                  Yes, that is correct. You should also free your worker objects:

                  QObject::connect(_moduleThread, &QThread::finished, _devicesManager, &QObject::deleteLater);
                  
                  A Offline
                  A Offline
                  Absurd
                  wrote on last edited by Absurd
                  #18

                  @kshegunov why not just delete _deviceManager after Qthread::wait() has returned?

                  @MrShawn said:

                  No, the first QString is like a "key" for your message type, so that you know what keys to look for or are available in your QVariantMap.

                  Can you please give a usage example for that?
                  I couldn't figure out why isn't void message(QString, QVariant) enough...

                  kshegunovK M 2 Replies Last reply
                  0
                  • A Absurd

                    @kshegunov why not just delete _deviceManager after Qthread::wait() has returned?

                    @MrShawn said:

                    No, the first QString is like a "key" for your message type, so that you know what keys to look for or are available in your QVariantMap.

                    Can you please give a usage example for that?
                    I couldn't figure out why isn't void message(QString, QVariant) enough...

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #19

                    @Absurd said in Motivation for multithreading in my App:

                    @kshegunov why not just delete _deviceManager after Qthread::wait() has returned?

                    It's possible, but it still belongs to the worker thread, you may get warnings.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1
                    • A Absurd

                      @kshegunov why not just delete _deviceManager after Qthread::wait() has returned?

                      @MrShawn said:

                      No, the first QString is like a "key" for your message type, so that you know what keys to look for or are available in your QVariantMap.

                      Can you please give a usage example for that?
                      I couldn't figure out why isn't void message(QString, QVariant) enough...

                      M Offline
                      M Offline
                      MrShawn
                      wrote on last edited by
                      #20

                      @Absurd

                      void message(QString msgType, QVariantMap data);
                      

                      QVariantMap not QVariant.

                      QVarantMap is a map with QStrings for keys, so like say you emit a signal that is declared like above, it may look something like this.

                      QVariantMap params;
                      int param1 = 2;
                      double param2 = 2.2;
                      params.insert("param1",param1);
                      params.insert("param2",param2);
                      emit message("runSomeSpecificMethod",params);
                      

                      Then the receiving slot may look something like this

                      void getMessage(QString messageType, QVariantMap data)
                      {
                           if (messageType.compare("runSomeSpecificMethod",Qt::CaseInsensitive) == 0){
                                SomeSpecificMethod(data.value("param1").toInt(),data.value("param2").toDouble());
                           }
                      }
                      
                      1 Reply Last reply
                      1

                      • Login

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