Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to pass output parameters through QtConcurrent?
mjepson last edited by
I have a function, that fills 2 vectors for me, with training data from cameras. Since it is a stereo setup and this calculation takes a bit of time for each camera, I want to do this parallel.
So I have now 2 QtConcurrent::run calls to the same function, passing different parameters. After starting these 2 threads, I wait for them to complete.
Since the function outputs 2 vectors, I pass these vectors as a reference parameter. I can see inside the function that the vectors are properly filled, yet these are empty again when I return to the main function, which was waiting for these threads to finish.
I can see how this makes sense, as using a reference parameter in another thread is obviously not very wise, since it might be deleted by the thread in which it was created. But now the question remains: what is the best way of using a function with output parameters in QtConcurrent::run?
rcari last edited by
You should pass pointers to your vectors instead of references. This is I think because the va_args_list of QtConcurrent::run() make implicit deep copies of the vectors (it calls the QVector<T>(const QVector<T>&) constructor behind the scenes).
Using pointers you are guaranteed to address the right vector.