I have a thread for a connection UDP.
I want to pause my thread while timeout (or while there isn't new frames in my QStack which stores UDP frames).
run(){
forever{
QTimer *timer_nb = new QTimer();
timer_nb->start(500);
// --- Wait for timeout or new frames in my QStack
//then ....
}
}
I found a function select(), but I don't really understand how it works ... Is there an alternative with Qt ?
(Sorry for my english)
The Qt mindset is most of the time to work with events, the Qt event loop will do the wait/pause for you. Though select (pselect on linux) does exactly what you want... it is not the Qt way.
A Qt way of doing it could be to connect to both signals of a new frame received and a timeout occured. If your objects are created in your UDP thread, their slots will be executed in the same thread (doc about this).
You can initialize in the run function and create+connect the signals to some custom slot(s), then instead of calling forever you just call exec() (doc). The run function will not return and therefore not terminate your thread, but will be waiting for events until you terminate the thread yourself by calling exit(). You can call it yourself when you have received what you want, or from the main thread to just stop processing the frames at some point.
Then you need a custom slot which process new frames and reset the QTimer to restart the timeout from 0. And eventually another custom slot to handle the timeout, or the same slot depending on what you have to do when it happens.
Now you can see your code is not blocking, so you could probably do all of this directly in the main thread, except if the data processing is long.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With