Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program stop responding when my QRunnable class does anything
QtWS25 Last Chance

Program stop responding when my QRunnable class does anything

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Qt Evellyn
    wrote on last edited by
    #1

    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));
    }
    threadpool->waitForDone();
    @

    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.
    Hints?

    Evellyn 【ツ】

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Qt Evellyn
        wrote on last edited by
        #3

        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.

        Evellyn 【ツ】

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          [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":http://qt-project.org/doc/qt-5.1/qtcore/threads-reentrancy.html

          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 http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html for different ways to use threads in Qt.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved