0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Go to [the documentation](https://doc.qt.io/qt-5/qtconcurrentrun.html) and read the "Passing Arguments to the Function" section. Then read about [std::reference_wrapper](https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper). – molbdnilo Feb 24 '20 at 08:12

1 Answers1

0

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.