Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Concurrent Map with different size of return sequence compared to input sequence.



  • Hi, I am new to QtConcurrent and I am trying to implement a function where I calculate values from a QVector and generate a new one. The problem is the return sequence QtConcurrent::mapped() will not be the same size as the input sequence. The size of the returned sequence depends on the value of the input sequence and the map function (which is a member function and uses a number of member variables to calculate the return sequence) which is operating on it. Is there any way to use QtConcurrent::mapped() with a function that does not have a return value but rather updates the QFuture object itself (or some other kind of hack to achieve a similar result, like combining QtConcurrent::filtered() and QtConcurrent::mapped())?



  • If your compiler supports C++17 you can do it using STL instead:

    QVector<int> input {1,2,3,4,5,6,7,8,9};
    QVector<int> output;
    std::mutex outputMutex;
    std::for_each(std::execution::par,input.cbegin(),input.cend(),[&output,&outputMutex](int val){if(val%2) return;  outputMutex.lock(); output << val*2;  outputMutex.unlock();});
    


  • @VRonin Thanks! Yes, my compiler supports C++17, I will try this method.


Log in to reply