qtconcurrent

Doesn't change value on reference


I'll try run few QtConcurrent, on function which must change value:

void MyClass::parse_data_request(QVector<event_discription> &event_vec, QString list_events, QString num, QStringList &loc_key_collector, QStringList &loc_key_ru)

And run throw this:

QVector<QFuture<void> > threads(thread_counts);
    for(int i = 0; i < thread_counts; i++)
        {
            threads[i] = QtConcurrent::run(this,&MyClass::parse_data_request, it_event[i], list_events[i], proxy[proxy_num + i],glob_key_collector[i], glob_key_ru[i]);
            qDebug() << "Run thread No " << i;
        }
        for(int i = 0; i < thread_counts; i++)
        {
            threads[i].waitForFinished();
        }

In function i append data to event_vec, but when

for(int i = 0; i < thread_counts; i++)
    {
        threads[i].waitForFinished();
    }

is done, it_event[i],glob_key_collector[i] and glob_key_ru[i] doesn't change, they have zero elements. What am I doing wrong?

P.S.: QtConcurrent::run called in another function of MyClass


Solution

  • Thanks to @molbdnilo for marking my misunderstanding of QtConcurrent::run Working version:

    threads[i] = QtConcurrent::run(this,&marathon_parser::parse_data_request, std::ref(it_event[i]), list_events[i], proxy[proxy_num + i],std::ref(glob_key_collector[i]), std::ref(glob_key_ru[i]));
    

    QtConcurrent::run make copy of ALL arguments of the Function by value, in that way we must use std::ref to pass the reference.