What to use, QueuedConnection or QMutex to make the object thread safe?

I am creating an application that needs to download thousands of HTML files, parse them, and then put them into something like a HashMap, which is global, and I decided to use multithreading to fix the speed.

So, the question is what should I use QueuedConnection for signals / slots or QMutex to make HashMap thread safe.

I used QueueConnection to make things simpler, I created a lot of sub-streams to load and pass the pointer back to the main thread, to parse them and put in the HashMap, it works great.

However, when I read some comments that QueueConnection really takes a lot of time, I started rebuilding my code and used QMutex to make my HashMap thread safe, then I can do all the work (load, put them in the HashMap) in subtopics.

But the results are not very optimistic, the latter method takes much longer than the first.

Is QueueConnection the best way to do this job?

Examples of codes are as follows:

Using QueuedConnection:

class Html
{
    void create();
    {
      /* Load from local file */
    }
    void analyze()
    {
        /* Pick out every word and put them into the inverted list */
        QString word = this->getNextWord();
        /* What stored in the hashmap is a list */
        List list = HashMap::globalInstance()->getList(word);
        /* Do some work like checking */
        list->append(this);
    }
}



class LoadHtml : public QThread
{
signals:
    void processHtml(Html* ptr);
public:
    void run()
    {
        Html* ptr = new Html();
        ptr->create();
        emit processHtml(ptr);
    }
}

class MainThread: public QThread
{
private:
    LoadHtml loadHtml;
slots:
    void processHtml(Html* ptr)
    {
        ptr->analyze();
    }
    void run()
    {
        connect(&loadHtml,LoadHtml::processHtml,this,MainThrad::processHtml,Qt::QueuedConnection);
        loadHtml.start();
    }
}

And QMutex version is similar to the simple removal of the signal / slot and place QMutex all methods HashMapand Listand try to do analyze()in LoadHtml.

+4
source share
1

, , , .

. , , , . / . . , , .

, , , . , , ? .

+1

Source: https://habr.com/ru/post/1614015/


All Articles