Using qDebug inside QtConcurrent::run()
-
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?
-
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();
}
@ -
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.
-
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();
}
@ -
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
-
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.
-
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?
-
[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.
-
@JKSH : thanks!
@qttester5 : What are the specs of your system?
-
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"