PDA

View Full Version : Qt and number crunching



The_Fallen
8th March 2010, 12:45
Hi there,

this is my first post to this forum, so important things first: Hello everyone! :-)
I'm using Qt for quite some time now, but of course there are always things to learn...
And I would like to start with this:

I'm doing some heavy duty number crunching, which can keep my poor computer busy for several hours quite easily. In between I would like to have a Qt window updated with some status messages and plots.
What I'm doing right now is put all the number crunching in a QThread and create the dialog in its constructor, so that it runs in the GUI thread. But to be honest I really think, this is the wrong way, since the number crunching should be the main thread and GUI in some low priority thread. But if I put all the GUI stuff into a separate thread, I run into problems, since I need to do all GUI calls from a single thread.
How do you handle this? Any help would be appreciated. Thanks.

Cheers,
Tim

squidge
8th March 2010, 13:09
I always have the GUI thread as the main thread of execution, and have all the number crunching in another thread. The threads then send back status information to the main thread using signals and slots. The GUI only ever updates when needed, so there's no need for it to be low priority, as it uses far less than 1% of the CPU time anyway as it spends most of the time sleeping.

In your case, I wouldn't create a dialog in the number crunching threads constructor - I would create a thread that does nothing but number crunching, and simply broadcasts status signals which the main thread would pickup and do things with.

The_Fallen
8th March 2010, 15:21
Thanks. I've just implemented this. The threads are now communicating only using signals/slots, which are completely thread-safe, aren"t they? But anyway, seems to work nicely, thanks again.

bmhautz
8th March 2010, 21:49
Thanks. I've just implemented this. The threads are now communicating only using signals/slots, which are completely thread-safe, aren"t they? But anyway, seems to work nicely, thanks again.

Yes, they are thread safe.