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. Legacy code: why sleep before restarting a QThread that is already running?

Legacy code: why sleep before restarting a QThread that is already running?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 334 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    In the legacy codebase that I work on, I see the following pattern twice in a wrapper function around QThread::start (Foo is derived from QThread):

    void Foo::fooStart()
    {
        ... do stuff ...
    
        while (isRunning())
        {
            // Keep wait until Qt internal flag set to false.
            QThread::msleep(QT_INTERNAL_FLAG_RESET_TIMEOUT);
        }
    
        ... do stuff ...
    
        start();
    }
    

    where

    const int QT_INTERNAL_FLAG_RESET_TIMEOUT = 10;
    

    So for some reason, if the thread is already running and we restart it (which according to https://doc.qt.io/qt-5/qthread.html#start would be a no-op) then for some strange reason we first wait for 10 milliseconds.

    I googled for the QT_INTERNAL_FLAG_RESET_TIMEOUT or 'Qt internal flag' or 'QThread internal flag' but that didn't help me explain this.

    Does anyone have an idea what is happening here? Is this some strange idiom that was required in earlier versions of Qt, or is it just unnecessary code that can be removed?

    jsulmJ 1 Reply Last reply
    0
    • B Bart_Vandewoestyne

      In the legacy codebase that I work on, I see the following pattern twice in a wrapper function around QThread::start (Foo is derived from QThread):

      void Foo::fooStart()
      {
          ... do stuff ...
      
          while (isRunning())
          {
              // Keep wait until Qt internal flag set to false.
              QThread::msleep(QT_INTERNAL_FLAG_RESET_TIMEOUT);
          }
      
          ... do stuff ...
      
          start();
      }
      

      where

      const int QT_INTERNAL_FLAG_RESET_TIMEOUT = 10;
      

      So for some reason, if the thread is already running and we restart it (which according to https://doc.qt.io/qt-5/qthread.html#start would be a no-op) then for some strange reason we first wait for 10 milliseconds.

      I googled for the QT_INTERNAL_FLAG_RESET_TIMEOUT or 'Qt internal flag' or 'QThread internal flag' but that didn't help me explain this.

      Does anyone have an idea what is happening here? Is this some strange idiom that was required in earlier versions of Qt, or is it just unnecessary code that can be removed?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Bart_Vandewoestyne said in Legacy code: why sleep before restarting a QThread that is already running?:

      Does anyone have an idea what is happening here?

      As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        This seems like a strange pattern. Wait until a job finishes (meanwhile blocking the caller), and then start it again. This effectively turns two threads of execution into one.

        One of the QtConcurrent functions, a semaphore, or a worker thread pattern would be easier to follow and more efficient. If nothing else, QThread::wait() saves on pointless wakeups.

        On the other hand, legacy codebase...

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @Bart_Vandewoestyne said in Legacy code: why sleep before restarting a QThread that is already running?:

          Does anyone have an idea what is happening here?

          As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.

          B Offline
          B Offline
          Bart_Vandewoestyne
          wrote on last edited by
          #4

          @jsulm said in Legacy code: why sleep before restarting a QThread that is already running?:

          As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.

          Oh yes... I was actually wrong when I wrote that we only wait for 10 milliseconds before restarting the thread... I must've been stilll sleeping I guess ;-) I misread the 'while' for 'if'... so you are right, it simply waits for the thread to finish (if it is already running) before starting it again.

          1 Reply Last reply
          2

          • Login

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