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. QElapsed does not work in a separate thread
Forum Updated to NodeBB v4.3 + New Features

QElapsed does not work in a separate thread

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 698 Views 3 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.
  • M Offline
    M Offline
    Merlino
    wrote on last edited by Merlino
    #1

    Hello,
    I'm trying to use a QElapsedTimer inside a std::thread to send processed data at regular time intervals in a way similar to the code below. Unfortunately the method elapsed() return alway 0. Can anybody help me?

    void MyClass::ConsumerThread()
    {
      QElapsedTimer elapsedTimer;
      elapsedTimer.start();
    
      while (!queue.isEmpty())
      {
        // <dequeue, process and accumulate>
    
        Q_ASSERT(elapsedTimer.isValid());
        if (elapsedTimer.elapsed() > 500) // elapsed() returns always 0
        {
          // ==> send processed data
          elapsedTimer.restart();
        }
      }
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      This is strange, can you try with std::chrono (example at the bottom of this page) and see if the same happens?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply
      1
      • VRoninV VRonin

        This is strange, can you try with std::chrono (example at the bottom of this page) and see if the same happens?

        M Offline
        M Offline
        Merlino
        wrote on last edited by
        #3

        @VRonin Thank you for your answer, with std::chrono doesn't work the same but something is slight different, so I have found the bug.
        The thread processing time in my test (really low data rate respect to production) it takes only few milliseconds, so the elapsed is not detected. The QElapsedTime version return always 0, the std::chrono most of the times 0 but sometimes 1 or 2ms (maybe is more precise or the std::chrono version consumes moretime).

        1 Reply Last reply
        0
        • O Offline
          O Offline
          ollarch
          wrote on last edited by
          #4

          If the processing time is less than one millisecond you could use "nsecsElapsed()" instead.

          1 Reply Last reply
          3
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            I have used to QElapsedTimer in a thread to monitor network code. However, the times I monitored were anywhere from 15 seconds to 500mS. So it does work fine in threads.

            C++ is a perfectly valid school of magic.

            O 1 Reply Last reply
            0
            • fcarneyF fcarney

              I have used to QElapsedTimer in a thread to monitor network code. However, the times I monitored were anywhere from 15 seconds to 500mS. So it does work fine in threads.

              O Offline
              O Offline
              ollarch
              wrote on last edited by
              #6

              @fcarney I'm using a lot of QElapsedTimer objects into a lot of threads without any problem.

              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