
Originally Posted by
ibex
Rather than blocking the calling thread you could use it to perform some of the computation.
That's what you should do - you schedule a bunch of jobs and continue with your normal operations. Then at some point when the results are ready you query for them and use them.
The point being in my example there was one (global) thread pool which had spawned the caller of QtConcurrent::map. I don't think there is any mechanism for map and the pool to determine that one of the pool spawned threads is blocked and therefore all cpu resources won't be used.
I think you still fail to get the picture. The pool contains 4 threads. You schedule some job so a thread is spawned reducing the available number of threads to 3. Then you schedule another job and since three threads from the pool are available at the moment, 3 will be used. When the first thread finishes execution, QtConcurrent will use it for the other operation if some work still needs to be done. This all has nothing to do with all other possible threads in your application.
On a unicore machine the situation is worse. maxThreadCount would be 1, but 1 thread would also be in use so the map's thread request would be queued indefinitely.
This is only the case if you schedule a blocking job from within a job already performed on the thread pool. Like so:
void func() {
QList<QImage> imgs = makeList();
QtConcurrent::blockingMapped(imgs, someFunc);
}
// ...
QtConcurrent::run(func);
void func() {
QList<QImage> imgs = makeList();
QtConcurrent::blockingMapped(imgs, someFunc);
}
// ...
QtConcurrent::run(func);
To copy to clipboard, switch view to plain text mode
This is why you have QThreadPool::releaseThread():
void func() {
QList<QImage> imgs = makeList();
QThreadPool::globalInstance()->releaseThread();
QtConcurrent::blockingMapped(imgs, someFunc);
QThreadPool::globalInstance()->reserveThread();
}
// ...
QtConcurrent::run(func);
void func() {
QList<QImage> imgs = makeList();
QThreadPool::globalInstance()->releaseThread();
QtConcurrent::blockingMapped(imgs, someFunc);
QThreadPool::globalInstance()->reserveThread();
}
// ...
QtConcurrent::run(func);
To copy to clipboard, switch view to plain text mode
Bookmarks