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. QFuture threads working on the same container.

QFuture threads working on the same container.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 326 Views
  • 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.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on last edited by
    #1

    I have to fill a huge QVector with data. For performance reasons, this should be done simultaneously by different threads. I have a (very simplified) example which appears to be working. But is it safe to do it in the following way?

    extern void fillDataVector(QVector<QString>&source, QVector<int>*target, int start, int end) {
        for (int i = start; i<end; i++) {
            (*target)[i]=source.at(i).toInt();
        }
    }
    int main(int argc, char *argv[]) {
        QVector<QString> source(100, "12345"); 
        QVector<int> target(source.count()); // initialized
        fillDataVector(source, &target, 0,50);
        fillDataVector(source, &target, 50,100);
        QFuture<void> future1 = QtConcurrent::run(fillDataVector, source, &target, 0,50); // no overlapping
        QFuture<void> future2 = QtConcurrent::run(fillDataVector, source, &target, 50,100); // no overlapping
    
        while (!future1.isFinished() || !future2.isFinished()) {   }
        qDebug()<<target;
        return 0;
    }
    
    

    As you see, both threads are working on the same QVector but on reliably different elements of it.

    I could, of course, alternatively let the fillDataVector function return small QVector chunks which I then append to a previously empty result after the futures are finished, but that would be a huge perfornmance loss.

    This is more or less my first shot with Threading, so I'd be happy for a good practice examples I could use for this case.

    JonBJ 1 Reply Last reply
    0
    • SeDiS SeDi

      I have to fill a huge QVector with data. For performance reasons, this should be done simultaneously by different threads. I have a (very simplified) example which appears to be working. But is it safe to do it in the following way?

      extern void fillDataVector(QVector<QString>&source, QVector<int>*target, int start, int end) {
          for (int i = start; i<end; i++) {
              (*target)[i]=source.at(i).toInt();
          }
      }
      int main(int argc, char *argv[]) {
          QVector<QString> source(100, "12345"); 
          QVector<int> target(source.count()); // initialized
          fillDataVector(source, &target, 0,50);
          fillDataVector(source, &target, 50,100);
          QFuture<void> future1 = QtConcurrent::run(fillDataVector, source, &target, 0,50); // no overlapping
          QFuture<void> future2 = QtConcurrent::run(fillDataVector, source, &target, 50,100); // no overlapping
      
          while (!future1.isFinished() || !future2.isFinished()) {   }
          qDebug()<<target;
          return 0;
      }
      
      

      As you see, both threads are working on the same QVector but on reliably different elements of it.

      I could, of course, alternatively let the fillDataVector function return small QVector chunks which I then append to a previously empty result after the futures are finished, but that would be a huge perfornmance loss.

      This is more or less my first shot with Threading, so I'd be happy for a good practice examples I could use for this case.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @SeDi said in QFuture threads working on the same container.:

      But is it safe to do it in the following way?

      I will stick out my neck and say this is fine --- so long as the size of the vector is pre-allocated and adequate, as in your case. I shall be interested if the experts correct me and say this is not so!

      1 Reply Last reply
      2

      • Login

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