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