PDA

View Full Version : proper method for dynamically creating QThreads



kemere
18th May 2011, 07:53
I have a GUI which displays data traces using opengl. To make everything work nice, I've moved the code which updates the data to another thread. So I have something like this:


class DataObjectGL : QWidgetGL {
public:
DataUpdate : QObject
}

DataObjectGL :: DataObjectGL() {

dataUpdate = new DataUpdate();
dataUpdate->moveToThread(&GUIDataProcessThread);

}

I would like to generate an arbitrary (small) number of these objects depending on the GUI layout.

I have found two ways of signalling the existence of new data. If I use a signal connected to a slot in dataUpdate, everything works great. However, for efficiency's sake, I'd like to use a QWaitCondition. However, that means that the data processing code must run in an infinite loop in dataUpdate. And when I start this loop (by connecting to the thread start() signal), the thread only gets to start the first object's loop. Am I misunderstanding QWaitCondition? Can multiple QWaitConditions not run in a single thread?

thanks!

Berryblue031
18th May 2011, 10:43
How is new data detected? If it is something you check after a fixed time you could run a QTimer and do the check then the processing on the timeout signal to avoid having threads waiting.

For spawning a bunch of processing threads I would recommend you check the documentation for QRunnable and QThreadPool, it will take care of managing the threads is an efficient way.

wysota
18th May 2011, 10:48
Wait conditions are not bound to a thread. They just provide a synchronisation point where the behaviour of threads is controlled using a mutex. You can have as many wait conditions per thread as you want but only one of them can restart the thread (the one a particular thread waits for). For what you want to do I'd probably recommend using QtConcurrent although it depends on what your data feeding thread will actually be doing.