Important: Please read the Qt Code of Conduct -

Loading image concurrently

  • Hi,

    I'm trying to load images from a directory. The amount of images can be a couple thousands, so I thought about doing it concurrently and save some IO blocking time. The trick it that I need to wait until all of the images are loaded. The code I've written gets into a deadlock sometime, and almost always when debugging, and I don't really understand why.

    Here's what I have -

         for (int i=0; i <filesToDisp.size(); ++i) {
            QString extenstion = filesToDisp[i].split('.').last();
            if (isImage(extenstion)) {
                QString name = filesToDisp[i];
                if (name.contains("/")) {
                    name = path + "/" + name.split("/")[1];
                } else {
                    name = path + "/" + filesToDisp[i];
                QFuture<QPair<QImage, QString>> f =
                        QtConcurrent::run(this, &imageManager::getImageByName, name, filesToDisp[i]);
        // Waiting for all run to finish
        bool flag = true;
        while (flag) {
            flag = false;
            for (auto& f : fs) {
                if (!f.isFinished()) {
                    flag = true;
                } else {
                    QPair<QImage, QString> p = f.result();
                    _imageQueue.enqueue(new IEimage(new QImage(p.first), p.second, getFreeId()));

    getImageByName returns a pair of image and string.

    Any ideas?

  • Lifetime Qt Champion

    @Jonathan-Levanon said in Loading image concurrently:


    You're modifying the container while looping over it. Better use iterators + erase(iterator) and simply check if the container is empty as abort condition instead the flag stuff.

  • Such a rookie mistake :) Thanks, it works!

  • @Jonathan-Levanon said in Loading image concurrently:

    Such a rookie mistake :) Thanks, it works!

    But you found it NOW and not have it create errors that are hard to detect later. It would have been much worse if you somehow "made it work".

Log in to reply