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. Using qDebug inside QtConcurrent::run()
Forum Updated to NodeBB v4.3 + New Features

Using qDebug inside QtConcurrent::run()

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 3.3k Views 1 Watching
  • 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
    qttester5
    wrote on last edited by
    #1

    I have a qDebug call at the top of a function that is launched via QtConcurrent::run(). I also have another qDebug call in the function on the calling thread that handles the result output when the run() ends. Despite that my function in run() takes a couple seconds, both these log calls print to the console simultaneously.

    Is it not possible to print a log statement in realtime from another thread?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      Hi,

      I made a quick test, and everything seems to work..

      @
      #include <QCoreApplication>
      #include <QtConcurrent>
      #include <QDebug>

      void doSomething() {

      qDebug() << "doSomething start";
      
      QThread::msleep(2000);
      
      qDebug() << "doSomething end";
      

      }

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

      qDebug() << "main start";
      
      QFuture<void> future = QtConcurrent::run(doSomething);
      
      qDebug() << "main wainting for end";
      
      future.waitForFinished();
      
      qDebug() << "main end";
      
      return a.exec&#40;&#41;;
      

      }
      @

      Steven CEUPPENS
      Developer &#x2F; Architect
      Mobile: +32 479 65 93 10

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

        Yes, I know the logging works. Note that my question is with regards to "realtime." What I want to know is if there is any reason why log statements will not appear until after the run() function completes, even if those log statements appears at the top of the function? Right now, if the run() take a couple seconds, the qDebug only prints to console after the run() finishes, even though it should print when the function begins.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stevenceuppens
          wrote on last edited by
          #4

          Thats wat i wanted to prove with my previous example :)

          When i run the test, it prints realtime.. (for me)

          can you try following example:

          @
          #include <QCoreApplication>
          #include <QtConcurrent>
          #include <QDebug>

          void doSomething() {

          qDebug() << "doSomething start";
          
          int count = 10;
          
          while(count) {
          
              qDebug() << "doSomething loop: " << count;
          
              count--;
          
              QThread::msleep(500);
          }
          
          qDebug() << "doSomething end";
          

          }

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

          qDebug() << "main start";
          
          QFuture<void> future = QtConcurrent::run(doSomething);
          
          qDebug() << "main wainting for end";
          
          future.waitForFinished();
          
          qDebug() << "main end";
          
          return a.exec&#40;&#41;;
          

          }
          @

          Steven CEUPPENS
          Developer &#x2F; Architect
          Mobile: +32 479 65 93 10

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qttester5
            wrote on last edited by
            #5

            I ran your test. See the attached. It confirms my problem. The logging is not happening when run() is called.

            Dropbox image:

            https://www.dropbox.com/s/xwfo0wbj3734ips/Screenshot 2014-04-21 21.20.02.png

            1 Reply Last reply
            0
            • JKSHJ Online
              JKSHJ Online
              JKSH
              Moderators
              wrote on last edited by
              #6

              Note that qDebug() is not thread-safe. I got this with the Qt 5.2.1 + MinGW 4.8 kit:

              !http://i.imgur.com/E4wBlUS.png(Concurrent log)!

              The "doSomething loop" lines were printed in real-time (a new line appears every 0.5s), but there are a few mangled lines at the top.

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stevenceuppens
                wrote on last edited by
                #7

                ok,

                To be clear;

                @JKSH: you confirm that at your test it also run realtime ("doSomething loop: n" show up every 500 ms)?

                @qttester5: at your system it doesn't, so all the lines are printed at once when the QtConcurrent:run() method returns?

                Steven CEUPPENS
                Developer &#x2F; Architect
                Mobile: +32 479 65 93 10

                1 Reply Last reply
                0
                • JKSHJ Online
                  JKSHJ Online
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="stevenceuppens" date="1398089659"]ok,

                  To be clear;

                  @JKSH: you confirm that at your test it also run realtime ("doSomething loop: n" show up every 500 ms)?[/quote]Yes.

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    stevenceuppens
                    wrote on last edited by
                    #9

                    @JKSH : thanks!

                    @qttester5 : What are the specs of your system?

                    Steven CEUPPENS
                    Developer &#x2F; Architect
                    Mobile: +32 479 65 93 10

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qttester5
                      wrote on last edited by
                      #10

                      I'm running Qt for Windows on a VM on a Macbook Pro. As soon as I launch this test, I see several lines already printed on the screen, and then there is a delay and the next line that prints is the one beginning with "do something loop 9". The line "do something start" occurs after the line "waiting for end"

                      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