mardi 21 avril 2015

Issue with QThreadPool: fail to finish all the task?

I have the following code:

#include <QCoreApplication>
#include "myserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyServer server;
    server.startServer();

    return a.exec();
}
/////////////////////////////////////////////////
#include "myserver.h"
#include "myrunnable.h"

MyServer::MyServer(QObject *parent)
{
    pool = new QThreadPool(this);
    pool->setMaxThreadCount(10);
}

void MyServer::startServer()
{
    for (int i = 0; i < 11; i++)
    {
        MyRunnable *task = new MyRunnable();
        task->setAutoDelete(true);
        pool->start(task);
    }
}
/////////////////////////////////////////////////
#include "myrunnable.h"
#include <qdebug.h>
MyRunnable::MyRunnable()
{
}

void MyRunnable::run()
{
    qDebug() << "Run";
}

I run my code hoping by calling the startServer function it will give me 11 Run print on the screen. But it never did so. It printed out some Run on the screen but never all of them.

And the program even crash sometimes due to the stackoverflow. I checked the call stack and it is caused by some strange QMutex recursive.

So I assumed there must be some serious threading related issue with my code due the usage of QThreadPool and QRunnable. But I cannot figure out what it is even after reading the Qt's Docs...

Aucun commentaire:

Enregistrer un commentaire