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();
{
}
void analyze()
{
QString word = this->getNextWord();
List list = HashMap::globalInstance()->getList(word);
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 HashMap
and List
and try to do analyze()
in LoadHtml
.