P
P
Pavel2015-09-19 19:25:38
Qt
Pavel, 2015-09-19 19:25:38

How to use QtConcurrent?

How to use QtConcurrent::map();
Yes
What is wrong in the code?

void MainWindow::processing_list(QStringList &line)
{
    //******
}
QStringList lst;
QtConcurrent::map(&lst,&this->processing_list);

The compiler complains:
no match for call to '(QtConcurrent::MemberFunctionWrapper1<void, MainWindow, QStringList&>) (QString&)'
         map(*it);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TriKrista, 2015-09-19
@TriKrista

Try to pass objects instead of references:

...
QtConcurrent::map(lst, this->processing_list);
...

...
this is how it works for me:
#include <QCoreApplication>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>
#include <QString>
#include <QDebug>

void test(QString &str) {
    qDebug() << str << " | ";
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QStringList strings;
    strings << "test" << "test1";
    QFuture<void> squeezedStrings = QtConcurrent::map(strings, &test);

    return a.exec();
}

and this is how it works for me:
//mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QStringList>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>
#include <QObject>

class MainWindow : public QObject
{
public:
    MainWindow(QObject *parent = 0);
    void start();
    QStringList lst;

private:
    static void processing_list(QString &line);
};

#endif // MAINWINDOW_H


//mainwindow.cpp

#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QObject *parent) : QObject(parent){

}

void MainWindow::start() {
    lst << "test" << "test1";
    QtConcurrent::map(lst, &this->processing_list);
}

void MainWindow::processing_list(QString &line) {
    qDebug() << line;
}

P
Pavel, 2015-09-20
@yankeenoobs

TriKrista : In general, this whole perversion did not suit me, so I wrote everything in QThread and looped through my portion of data on each processor, but I did not notice an increase in acceleration, rather, on the contrary. I suspect that the whole point is that each core in a loop bypasses the list, and each new element is searched in turn, because QList objects

ParsingFile::ParsingFile(QList<QString> &list_string, int num_core, QObject *parent): QThread(parent)
{
    this->row = &list_string;
    this->number_core = num_core;
    //qDebug() << "start core: " << number_core << endl;
}
void ParsingFile::run()
{
    int maxThread = QThread::idealThreadCount();
    for(int p=this->number_core; p < this->row->count(); p=p+maxThread)
    {
        QString tmp = row->at(p);
        DATA_Channel T;
        QString arg= "";
        bool quote = false;
        int num_arg = 0;
        for(int i=0; i<tmp.length(); i++)
        {
           ******
        }
        this->channel.push_back(T);
    }
    emit result_parse(this->channel);
}

Threads are created like this
int core = QThread::idealThreadCount();
    for(int i=0; i<core; i++)
    {
        this->threads.push_back( new ParsingFile(row_lst,i) );
        this->threads[i]->start(QThread::HighestPriority);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question