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. What is the best way to implement a looped function with a timer?

What is the best way to implement a looped function with a timer?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.3k 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.
  • P Offline
    P Offline
    ptitz
    wrote on last edited by ptitz
    #1

    Basically I have a function that has to execute with a certain frequency(something like 50 Hz). I can either implement it with QThread, using an infinite loop() with Qthread::msleep(timestep) in the middle OR I could use a QTimer, sending a signal to loop() at fixed timestep. What is the "proper" way to deal with this? Which one will be more efficient, computationally?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      It depends partly on how long your operation takes.

      If you take the QTimer approach, you don't need to implement a loop() function, just a function that does what you need. You can use the worker object approach described in QThread's documentation. You also don't need a QTimer, QObject has startTimer + timerEvent that you can use for that purpose. That's up to your personal taste which one you're more comfortable to use.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        It depends partly on how long your operation takes.

        If you take the QTimer approach, you don't need to implement a loop() function, just a function that does what you need. You can use the worker object approach described in QThread's documentation. You also don't need a QTimer, QObject has startTimer + timerEvent that you can use for that purpose. That's up to your personal taste which one you're more comfortable to use.

        Hope it helps

        P Offline
        P Offline
        ptitz
        wrote on last edited by ptitz
        #3

        @SGaist OK, so there is not much difference and it's up to me then. It helps, thank you!

        kshegunovK 1 Reply Last reply
        0
        • P ptitz

          @SGaist OK, so there is not much difference and it's up to me then. It helps, thank you!

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

          @ptitz said:

          I can either implement it with QThread, using an infinite loop() with Qthread::msleep(timestep) in the middle

          Please, don't do that, it's bad programming. How are you going to stop the thread, by killing it? If you want to use the low-level threading API, create your worker QObject move it to the thread and signal it with a timer event.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gyll
            wrote on last edited by gyll
            #5

            Keep in mind that every event handler that you'll run in the main thread ads up to the time the main event loop requires to complete (and this directly impacts on the delay between the moment an event is detected and the time it gets to be processed). My personal approach is to use a different thread for each and every event handler in an application whenever possible (i.e. whenever the event handlers are truly independent) if there is a chance that said event handlers may take a relatively long time to complete.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ptitz Depends on which difference you are talking about, do you mean between a QTimer and using the startTimer/timerEvent ? If so, then yes, the difference is pretty minor.

              Otherwise, I agree with @kshegunov, for tasks that should run at regular interval, the worker object approach is better but you also have to be sure that the task can complete between two timeouts.

              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
              0

              • Login

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