PDA

View Full Version : Signal response speed



^NyAw^
30th January 2007, 11:17
Hi,
I want to know if the SLOT connected to a SIGNAL is executed just when the SIGNAL is emmited or there is a time between.

I have a program that have a Thread that process images and emits signals like Images, Lines, Circles, ... then the main thread have a slot for every signal and paint them into a widget. Sometimes, if I start the program, stop it, start, stop, start, ... I see that some images are arriving too late.
For example:
The thread emits the image, process this image and when it is processing, it emits signals that correspond to lines, circles, ...
Then when I stop and start the thread again, it seems that the lines and circles don't correspond to the image that is showing.

It's a program that works fast and sometimes it can process 25 images per second.

munna
30th January 2007, 11:30
A slot is immediately called when the corresponding signal is emitted. It might be drawing problem, not sure though.

wysota
30th January 2007, 11:43
A slot is immediately called when the corresponding signal is emitted. It might be drawing problem, not sure though.

This is true only for signals where objects emitting and receiving are within the same thread. Otherwise signals are queued and slots are fired when control reaches the event loop in the receiving thread.

^NyAw^
30th January 2007, 12:17
Hi,
I know that the slot is queued if the emitting thread and reciver are different.
Is there anyway to increase the main thread priority? I'm able to do it with different threads but I can't find how to do this with the main thread. Also I have searched to increase the process priority but don't find how.

Thanks,

wysota
30th January 2007, 12:31
What would increasing the priority change? That would slow down the worker threads slowing down signal emition. If you want to fire slots faster just make sure main thread doesn't do any long operations on its own (so that it can process the loop very quickly).

^NyAw^
30th January 2007, 12:38
Hi,
OK.
The main thread don't does anything else that respose to the signals emmited by the thread.
I will take a look at the thread code to see if there is any problem when I stop the thread.

Thanks,

wysota
30th January 2007, 13:01
If it doesn't do anything else then maybe you don't need worker threads at all?

^NyAw^
30th January 2007, 14:29
Hi,
Oh yes I need. I have 2 worker threads, one grabbing images from a webcam and another processing images from the buffer.

wysota
30th January 2007, 14:50
Do you have a multiprocessor (or multicore) machine? If no, then there is no efficiency benefit from using threads.

^NyAw^
30th January 2007, 15:15
Hi,
I have a Pentium 4 with HyperThreading but I will change it to a multicore in a few months.

I use threads because I don't want to freeze the main thread during image processing. Think that sometimes an image processing is about 2 or 3 seconds depending on the algorithm. This is why I have designed 2 threads, one grabbing images into a buffer and another one processing the images from the buffer.

TheRonin
31st January 2007, 09:25
I may be entirely off the mark here, but can't you set up your connections to be Qt::DirectConnection, even over different threads?


connect(this, SIGNAL(signal()), that, SLOT(slot()), Qt::DirectConnection);

That way when you call the signal execution immediately jumps to the other thread's slot. Granted, this will remove some of the reason for using two threads to begin with, but it should still keep the GUI responsive.

^NyAw^
31st January 2007, 09:39
Hi,
Thanks for reply but it is not possible to make direct connections between differrent threads.

Thanks,

TheRonin
31st January 2007, 14:52
I double checked the documentation and you're absolutely right, you can't create a direct connection between two separate threads. If i understand the docs correctly, you should still be able to make a direct connection and access methods and memory in the other thread - while retaining execution control in the original thread. Well, you can but it's not safe since the other thread's event loop - if there is one - may access the same memory at the same time.

From the documentation:

With direct connections, the slot gets called immediately when the signal is emitted. The slot is executed in the thread that emitted the signal (which is not necessarily the thread where the receiver object lives).

Be aware that using direct connections when the sender and receiver live in different threads is unsafe if an event loop is running in the receiver's thread, for the same reason that calling any function on an object living in another thread is unsafe.

But please, correct me again if i'm wrong. Thanks :D :confused:

wysota
31st January 2007, 15:00
In such situations I always suggest to ask yourself a question "do I really need it?", so do you really need "instant" slot responses? Is it just a thing you'd like to have or are you convinced it is crucial to the functioning of the application?