Important: Please read the Qt Code of Conduct -

Program stop responding when my QRunnable class does anything

  • Hello World!
    I'm trying to make a program that test the availability of some pages of a website.
    I would like to make the tests run in parallel, using QRunnable class.
    So in the main thread we have something like this:
    threadpool = new QThreadPool(this);
    foreach(QString pageAddress, listOfAllPageAddress) {
    threadpool->start(new PageTester(pageAddress));

    and the declaration of PageTester :

    @class PageTester : public QObject, public QRunnable@

    but if the method run do anything, even a QMessageBox::information() call, the program stop responding.

    I tested it on Win 7 ultimate with Qt 5.1.1 and 5.2.0 beta 1 for MinGW 4.8. Same behaviour.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    waitForDone will block the calling thread and wait until each thread of the thread pool finishes.

    If you want to keep a responsive UI you have to handle this differently or maybe use QtConcurrent.

    Hope it helps

  • Thank you SGaist.
    I tryed to remove waitForDone and the main thread becomes responsive.
    Other threads still refuse to obey me and the program crash. =(
    There's something I didn't mentioned early, because I though it was irrelevant to the problem:

    during the instantiation of QRunnable, I pass a pointer of QAccessManager to constructor.
    @threadpool->start(new PageTester(pageAddress, networkAccessManager));@

    I guess we can't use the same instance of QObject in two+ different threads...

    I will take a closer look to QtConcurrent.

    For now, I changed the approach to solve the problem.
    I made a simpler program that tests only 1 page, and then I make lots of instances of this program. Not the most "performatic" approach, but does the job.

    Thanks for the reply.

  • Moderators


    [quote author="Qt Qt Evellyn" date="1381955214"]I guess we can't use the same instance of QObject in two+ different threads...[/quote]No you can't. QObjects are not "thread-safe":

    In your case, QNetworkAccessManager already automatically runs up to 6 HTTP requests in parallel.

    Qt Concurrent won't provide anything new. QtConcurrent::run() behaves similarly to QRunnable. The other parts of QtConcurrent are for parallel processing of containers (QVector, QList, etc.)

    See for different ways to use threads in Qt.

Log in to reply