How to use Qt multithreading for parallel list processing?

Viewed 52

I'm using qt to make software that analyses a large amount of data. The data consist of individual "Uber" orders with information such as order time, start location, and end location, and I need to be able to evaluate the data such as plotting the graph of demand over time.

To do this, I have to check every record of the data and sum it onto a new data table according to its timestamp, this takes a long time so my initial solution is to use QtConcurrent::filterReduced to get my sum.

However, the filter function cannot take extra arguments to filter the data based on the time interval I want.

My question is, is there another quick and easy solution for this kind of problem? Or do I need to use QThread's low-level API for this, if so, any examples/tutorials on how I can achieve that?

1 Answers

Instead of passing a function, you can pass a function object which holds the "parameter". Something like this (T is your datatype here):

struct FilterWithTime
{
    FilterWithTime(const QString &filterPredicate)
    : m_filterPredicate(filterPredicate) { }

    typedef bool result_type;

    bool operator()(const T &value)
    {
        ... test value against filterPredicate
    }

    QString m_filterPredicate;
};

QtConcurrent::filterReduced<ResultType>(your-list-of-T, FilterWithTime(QString("10-12"), YourTransformationObject()));

Note the explicit instantiation with ResultType!!

Related