Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loading image concurrently
Forum Updated to NodeBB v4.3 + New Features

Loading image concurrently

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 435 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonathan Levanon
    wrote on 13 May 2019, 14:30 last edited by
    #1

    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]);
                fs.append(f);
            }
        }
    
        // 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()));
                    fs.removeOne(f);
                }
            }
        }
    

    getImageByName returns a pair of image and string.

    Any ideas?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 May 2019, 14:47 last edited by
      #2

      @Jonathan-Levanon said in Loading image concurrently:

      fs.removeOne(f);

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • J Offline
        J Offline
        Jonathan Levanon
        wrote on 13 May 2019, 15:07 last edited by
        #3

        Such a rookie mistake :) Thanks, it works!

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 13 May 2019, 15:43 last edited by
          #4

          @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".

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0

          1/4

          13 May 2019, 14:30

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved