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 to ensure thread's run function executes first
Qt 6.11 is out! See what's new in the release blog

How to ensure thread's run function executes first

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.3k Views 2 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.
  • N Offline
    N Offline
    nitks.abhinav
    wrote on last edited by
    #1

    Hi ,

    I have the below code where I am using threads started signal to ensure run function of the clientThread executes first and then any other function. All initialization are done in the run function and if any other function (serverConnect fn as below) executes first it creates issues.
    But using logs I see that 1 in 10 times run function of clientThread executes after serverConnect().

    Thank you,

    void Base::drpThreadStarted()
    {
        /* Once DRP thread is started , start the client thread. */
        connect(clientThread,SIGNAL(started()),this,SLOT(clientThreadStarted()));
        clientThread->start();
    }
    
    
    void Base::clientThreadStarted()
    {
        /* Connect to the notification server. */
        clientThread->serverConnect();
    
        /* Connect to the message queue in watchdog. */
        clientThread->watchdogConnect();
    
    }
    
    
    JonBJ kshegunovK 2 Replies Last reply
    0
    • N nitks.abhinav

      Hi ,

      I have the below code where I am using threads started signal to ensure run function of the clientThread executes first and then any other function. All initialization are done in the run function and if any other function (serverConnect fn as below) executes first it creates issues.
      But using logs I see that 1 in 10 times run function of clientThread executes after serverConnect().

      Thank you,

      void Base::drpThreadStarted()
      {
          /* Once DRP thread is started , start the client thread. */
          connect(clientThread,SIGNAL(started()),this,SLOT(clientThreadStarted()));
          clientThread->start();
      }
      
      
      void Base::clientThreadStarted()
      {
          /* Connect to the notification server. */
          clientThread->serverConnect();
      
          /* Connect to the message queue in watchdog. */
          clientThread->watchdogConnect();
      
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @nitks.abhinav

      using threads started signal

      At a guess, I would assume the signal indicates the thread has been started, but not that it has therefore already executed some instruction, even if it's the first one! So every so often it doesn't.

      If you need to be sure of the order of instructions across threads, you need some synchronising (mutex, semaphore, etc.) mechanism.

      No?

      1 Reply Last reply
      0
      • N nitks.abhinav

        Hi ,

        I have the below code where I am using threads started signal to ensure run function of the clientThread executes first and then any other function. All initialization are done in the run function and if any other function (serverConnect fn as below) executes first it creates issues.
        But using logs I see that 1 in 10 times run function of clientThread executes after serverConnect().

        Thank you,

        void Base::drpThreadStarted()
        {
            /* Once DRP thread is started , start the client thread. */
            connect(clientThread,SIGNAL(started()),this,SLOT(clientThreadStarted()));
            clientThread->start();
        }
        
        
        void Base::clientThreadStarted()
        {
            /* Connect to the notification server. */
            clientThread->serverConnect();
        
            /* Connect to the message queue in watchdog. */
            clientThread->watchdogConnect();
        
        }
        
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        You're running both of those in the same thread, moreover it probably isn't any of the threads you think ...
        Firstly, read this and this, and secondly post more of the code, namely how drpThreadStarted is invoked and how the Base instance is created.

        Read and abide by the Qt Code of Conduct

        N 1 Reply Last reply
        1
        • kshegunovK kshegunov

          You're running both of those in the same thread, moreover it probably isn't any of the threads you think ...
          Firstly, read this and this, and secondly post more of the code, namely how drpThreadStarted is invoked and how the Base instance is created.

          N Offline
          N Offline
          nitks.abhinav
          wrote on last edited by
          #4

          Base is QMainwindow class and it should not be the issue because we are serializing the flow using started() signal and also drpThread and clientThreads are threads only as below:

          Base::Base(QWidget *parent) :
                QMainWindow(parent, Qt::FramelessWindowHint)
          {
          #if MAIN_THREAD_DEBUG
                 qDebug() << "Base CTOR called";
          #endif
                 setStyleSheet("background-color:gray");
                 setAutoFillBackground( true );
          
                 drpThread  = new DRPThread;
                 clientThread = new ClientThread;
                 presenTimer = NULL;
                 nextGuiTimer = NULL;
          }
          
          class DRPThread : public QThread
          {
              Q_OBJECT
          
          signals:
          ...
          
          private:
             void run();
             Base *base;
          public:
          private slots:
              void rcvMsg();       //Recieve message from client thread
              void threadFinished();
              void initDataTimerHit();
          };
          
          class ClientThread : public QThread
          {
              Q_OBJECT
          
          signals:
          ...
          private:
              void run();
              QTimer *ka_timer;
              QTimer *conn_retry_timer;
          
          public:
              ....
              void serverConnect();
              void watchdogConnect();
          
          private slots:
          .....
              void threadFinished();
          
          private:
           ;
          };
          
          kshegunovK 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            From the looks of it, you are going to try to access a GUI element from DRPThread which is wrong.

            That noted, if you want to trigger things one after the other then you should signal precisely when you're done with the initialisation phase.

            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
            1
            • N nitks.abhinav

              Base is QMainwindow class and it should not be the issue because we are serializing the flow using started() signal and also drpThread and clientThreads are threads only as below:

              Base::Base(QWidget *parent) :
                    QMainWindow(parent, Qt::FramelessWindowHint)
              {
              #if MAIN_THREAD_DEBUG
                     qDebug() << "Base CTOR called";
              #endif
                     setStyleSheet("background-color:gray");
                     setAutoFillBackground( true );
              
                     drpThread  = new DRPThread;
                     clientThread = new ClientThread;
                     presenTimer = NULL;
                     nextGuiTimer = NULL;
              }
              
              class DRPThread : public QThread
              {
                  Q_OBJECT
              
              signals:
              ...
              
              private:
                 void run();
                 Base *base;
              public:
              private slots:
                  void rcvMsg();       //Recieve message from client thread
                  void threadFinished();
                  void initDataTimerHit();
              };
              
              class ClientThread : public QThread
              {
                  Q_OBJECT
              
              signals:
              ...
              private:
                  void run();
                  QTimer *ka_timer;
                  QTimer *conn_retry_timer;
              
              public:
                  ....
                  void serverConnect();
                  void watchdogConnect();
              
              private slots:
              .....
                  void threadFinished();
              
              private:
               ;
              };
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              Read the links I posted above and also this document: http://doc.qt.io/qt-5/thread-basics.html

              Base is QMainwindow class and it should not be the issue because we are serializing the flow using started() signal

              You think you do, but you're not. Both of those two slots are queued through the main thread's event loop, and also the slots in the QThread subclasses (unless invoked directly) are going to be executed in the main thread.

              Read and abide by the Qt Code of Conduct

              N 1 Reply Last reply
              2
              • kshegunovK kshegunov

                Read the links I posted above and also this document: http://doc.qt.io/qt-5/thread-basics.html

                Base is QMainwindow class and it should not be the issue because we are serializing the flow using started() signal

                You think you do, but you're not. Both of those two slots are queued through the main thread's event loop, and also the slots in the QThread subclasses (unless invoked directly) are going to be executed in the main thread.

                N Offline
                N Offline
                nitks.abhinav
                wrote on last edited by
                #7

                Ok Thanks.

                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