不改变参考价值
Doesn't change value on reference
我会尝试 运行 几个 QtConcurrent,在必须更改值的函数上:
void MyClass::parse_data_request(QVector<event_discription> &event_vec, QString list_events, QString num, QStringList &loc_key_collector, QStringList &loc_key_ru)
并且 运行 抛出这个:
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();
}
在函数中我将数据附加到 event_vec,但是当
for(int i = 0; i < thread_counts; i++)
{
threads[i].waitForFinished();
}
完成,it_event[i],glob_key_collector[i]
和 glob_key_ru[i]
没有改变,它们有零个元素。我做错了什么?
P.S.: QtConcurrent::run 在 MyClass
的另一个函数中调用
感谢@molbdnilo 指出我对 QtConcurrent::run 的误解
工作版本:
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 按值复制函数的所有参数,这样我们必须使用 std::ref 来传递引用。
我会尝试 运行 几个 QtConcurrent,在必须更改值的函数上:
void MyClass::parse_data_request(QVector<event_discription> &event_vec, QString list_events, QString num, QStringList &loc_key_collector, QStringList &loc_key_ru)
并且 运行 抛出这个:
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();
}
在函数中我将数据附加到 event_vec,但是当
for(int i = 0; i < thread_counts; i++)
{
threads[i].waitForFinished();
}
完成,it_event[i],glob_key_collector[i]
和 glob_key_ru[i]
没有改变,它们有零个元素。我做错了什么?
P.S.: QtConcurrent::run 在 MyClass
的另一个函数中调用感谢@molbdnilo 指出我对 QtConcurrent::run 的误解 工作版本:
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 按值复制函数的所有参数,这样我们必须使用 std::ref 来传递引用。