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
QVectorand generate a new one. The problem is the return sequenceQtConcurrent::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 useQtConcurrent::mapped()with a function that does not have a return value but rather updates theQFutureobject itself (or some other kind of hack to achieve a similar result, like combiningQtConcurrent::filtered()andQtConcurrent::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();}); -
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();});