Unsolved Timers in Qt
-
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
-
@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.
QTimer
s 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.
-
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.
-
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?