【问题标题】:Doesn't change value on reference不改变参考值
【发布时间】:2020-02-24 08:04:31
【问题描述】:

我会尝试在必须更改值的函数上运行几个 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 的另一个函数中调用

【问题讨论】:

标签: qtconcurrent


【解决方案1】:

感谢@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 来传递引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多