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. Timers in Qt

Timers in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 447 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.
  • G Offline
    G Offline
    Gaetano03
    wrote on last edited by
    #1

    Hello everyone,

    as I would like to run away from spawning threads here and there, I was trying to mitigate using QTimer instead, since my parallel events just consist of independent events which can be executed farily quickly and some waiting time. Therefore I created a timer for each process and the following two slots to catch the timeout

    void MainWindow::on_is_test_1_finished()
    {
    qDebug() << "Test 1 finished";
    }

    void MainWindow::on_is_test_2_finished()
    {
    qDebug() << "Test 2 finished";
    }

    Now, I was thinking to use a counter to keep track of the tests finished, so I can decide when the overall process is over. Therefore my slots should look like this

    void MainWindow::on_is_test_1_finished()
    {
    qDebug() << "Test 1 finished";
    count++;
    }

    void MainWindow::on_is_test_2_finished()
    {
    qDebug() << "Test 2 finished";
    count++;
    }

    and somewhere else (if count == numberoftests-> the test is finished)

    I was wondering if this is ok or there might be problems if test1 and test2 are perfectly synchronized, which is rather impossible, but...

    Thanks for your help

    JonBJ 1 Reply Last reply
    0
    • G Gaetano03

      Hello everyone,

      as I would like to run away from spawning threads here and there, I was trying to mitigate using QTimer instead, since my parallel events just consist of independent events which can be executed farily quickly and some waiting time. Therefore I created a timer for each process and the following two slots to catch the timeout

      void MainWindow::on_is_test_1_finished()
      {
      qDebug() << "Test 1 finished";
      }

      void MainWindow::on_is_test_2_finished()
      {
      qDebug() << "Test 2 finished";
      }

      Now, I was thinking to use a counter to keep track of the tests finished, so I can decide when the overall process is over. Therefore my slots should look like this

      void MainWindow::on_is_test_1_finished()
      {
      qDebug() << "Test 1 finished";
      count++;
      }

      void MainWindow::on_is_test_2_finished()
      {
      qDebug() << "Test 2 finished";
      count++;
      }

      and somewhere else (if count == numberoftests-> the test is finished)

      I was wondering if this is ok or there might be problems if test1 and test2 are perfectly synchronized, which is rather impossible, but...

      Thanks for your help

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Gaetano03 said in Timers in Qt:

      or there might be problems if test1 and test2 are perfectly synchronized, which is rather impossible

      What do you mean by this "synchronized"? If you are thinking that on a timer both slots could "be run at the same time", they cannot in Qt. Slots are called directly (normal case) or from the event loop (if queued), either way the slot has to run to completion and return before another slot (in the same thread) will be called. QTimers are not some kind of "hardware interrupt", rather the message loop sees them expire and calls their slots synchronously.

      TL;DR: Slots do not run at the same time as each other. You won't have a "race condition" on count++, if that is what you are thinking.

      OTOH:

      my parallel events just consist of independent events which can be executed farily quickly and some waiting time

      If you think your events are going to run "in parallel" when you are calling each from a different timer slot, they are not. Still one at a time. If you really want "parallel" (including if your events include "waiting time") then you would need threads.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wsma
        wrote on last edited by
        #3

        I am not sure about your question, however:

        Qt has a cooperative multitasking timer from a main loop. In other words, both timers cannot run simultaneously. There will be no conflicts on the variable increment.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gaetano03
          wrote on last edited by
          #4

          As of now,

          I am having two different Qt Objects, which do some preliminary settings sequentially and then each of them start a timer (still sequentially). Simply I have in the mainwindow class two sequential lines

          timertest1.start(timetofinish)
          timertest2.start(timetofinish)

          and I catch the two timeout signlas in the corresponding slots, where I perform some steps to close each test. While the timers are running I am actually doing nothing in each test (just waiting for the time to elapse), that's why I was saying maybe threads are not worth it in this scenario, where I can perform initial settings for the two tests sequentially.

          Does that make more sense and seems reasonable from an implementation point of view?

          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