PDA

View Full Version : QtConcurrent: clarifications needed



papillon
27th April 2012, 12:10
I've followed this example: http://codejourneys.blogspot.it/2008/06/qt-simple-example-of-use-of.html that shows how to use QtConcurrent to execute a function in a secondary thread.

Googling around, I've read somewhere that the QtConcurrent class would actually "balance" and span the workload on all available cores automatically. But after trying and reading some more, I think I understood that it's not really the case.
QtConcurrent::run() will actually run a function in a secondary thread, and you have to use it multiple times to enable more threads. Is this correct?

If so, say you have a myFunction performing n square roots, would you do something like this:



for (int i=0; i< QThreadPool::globalInstance()->maxThreadCount(); i++)
{
itemsPerThread=n/QThreadPool::globalInstance()->maxThreadCount();
itemStart=i*itemsPerThread;
itemEnd=(i+1)*itemsPerThread;
QtConcurrent::run(myFunction(itemStart,itemEnd));
}


i.e., assigning a range of values to process for each thread?

Thanks for any help.

pkj
27th April 2012, 15:26
As far as my understanding goes, a QThreadPool acquires as many threads as possible with the given infrastructure. As and when the functions are called to be executed in a different thread, it is provided. Once the pool gets filled, the functions are essentially serialized(behaviour akin to single threaded in a way). What I like about QtConcurrent is it very easier to program with it. For every function I need to run in a thread, i need not have a separate QThread class for it. It is closer to the behaviour of c# and java. And it was a very easy switch for me.
Your question seems to be of a Single Instruction multiple data. For each data in the container, the function will be called in a separate thread if possible. If not possible, some of them may be queued. I think of it as fire and forget system. And is quite efficient.

papillon
27th April 2012, 18:14
Thanks for the answer, but I still don't understand: is QtConcurrent supposed to automatically span over multiple threads, or do I have to setup n QtConcurrent threads?

pkj
28th April 2012, 06:05
Nope you don't have to do anything. It will be done for you.

Statix
28th April 2012, 16:34
QtConcurrent uses the QApplication global threadpool which automatically queries the number of logical cores.

QThreadPool::globalInstance()->setMaxThreadCount(100) will let your QtConcurrent::run functions over allocate the number of threads which can execute concurrently (with thread context switches that is).

I had a program which needed to spawn 100 or so QtConcurrents which do different things (not iterating over a map but dynamically invoking objects as an aggregate task) each thread semi-depended on the completion of another so I needed to have all the threads start at once and block until signalled. Maybe this is closer to the functionality you need.

papillon
28th April 2012, 18:17
Thank you very much for the suggestions, I now got QtConcurrent to work and use all the cores available.
I suppose I need to use QFuture and QFutureWatcher to eventually check if threads have finished, and to manually stop those?

Also, I have a Mac Pro dual processor: QtConcurrent will use all the cores from the first CPU only, while the second is ignored. Is perhaps a Qt bug (or, better, a missing feature) ?