Introducing Qt Concurrent

Published Friday February 23rd, 2007
9 Comments on Introducing Qt Concurrent
Posted in Labs, Qt Concurrent, Threads

As a part of Trolltech Labs, I’m adding the Qt Concurrent project. Qt Concurrent is a high-level threading framework for writing code that automatically scales on multi-core systems.

For example, to make thumbnails of a list of images you can do this:

QImage scaled(const QImage &image)
    return image.scaled(QSize(100, 100));


const QList<QImage> images = ...
const QList<QImage> thumbnails = QtConcurrent::mapped(images, scaled);

QtConcurrent::mapped() runs scaled() on each image in the list, using as many threads as there are CPU cores on the system.

In addition to this Qt Concurrent also provides a MapReduce implementation for shared-memory systems and support for making asynchronous function calls using futures.

You can get the source code at the labs pages, and there is also a forum for questions and general feedback.

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Labs, Qt Concurrent, Threads


taskmanager.cpp seems to require private/qobject_p.h. What is the best way to handle this dependency? Should I include the directory from the Qt source containing this file ($QT_SRC_DIR/src/corelib/kernel) ? Is that a portable solution?

brad says:

For now, yes, that is the best solution. The TaskManager uses the same private mechanism as the rest of Qt, which is why it needs qobject_p.h

Erik H. Bakke says:

Hi, Trolls.

I am currently in the process of porting an image processing package and photo database to Qt 4.2, and this framework caught my interest.
One of the things I have been struggling with for a while is to break up some particularly large image processing and conversion jobs into multiple work packages for faster processing on SMP systems.

I’ll be having a look at the Qt Concurrent framework to see if it will help me achieve this in an easier way.

Morten says:

Jacob: The qobject_p.h dependency is not really needed, so I went ahead and removed it. If you download a new package or sync there should be no compile errors.

Erik: This sounds like a very interesting use case for Qt Concurrent, please let us know how it works out!

Morten, thanks, it works great now. On my 2GHz dual core machine, the compress example is 90X faster when run with concurrency!

In the progress example QProgressDialog::setRange(int,int) is called as slot. It isn’t one in Qt4.2.X, is it in 4.3? What binary compatibility implications does that have?

Actually, repeated testing has shown the compress example to be 2-4X as fast with concurrency, as would be expected. My initial tests seem to have been some sort of fluke, the file I used seems to have some odd properties that trigger a slow codepath in zlib somehow…

Morten says:

I wish we could get 90X speedups, but this was probably due to disk cashing. The first time you run the compress example the single-threaded code path has to pay the cost of reading the data from disk, while the concurrent one can read it from the cache much faster. If you run it again both paths read it from cache and you get a truer speedup value.

QProgressDialog::setRange(int,int) is a slot in 4.3, and this is fully binary compatible.

robl says:

This looks like an interesting project. What I would really like to see is task pool based thread scheduler. You would allocate a series of tasks to a queue and the scheduler would distribute them amongst available threads taking note of available cores and task type (ie. you can specify whether a task is cpu intensive or i/o intensive and avoid waiting on low overhead gui threads) to maximise performance. Is this a direction you could see Qt Concurrent going?

Morten says:

There is a task scheduler/manager API in Qt Concurrent already ๐Ÿ™‚ Have a look at QtConcurrent::run() in run.h and the TaskManager class in private/taskmanager.h. There is currently no support for differentiating between cpu-bound, i/o-bound and “waiting” tasks, but this is certainly a fetaure I’d like to add.

Commenting closed.

Get started today with Qt Download now