Unsolved Why this thread prints out nothing
-
@JNBarchan Yep, that's why I added the full example in an edit. Also, must be noted, I don't think
qDebug()
is thread safe so I switched to good oldstd::cerr
-
@VRonin
Yep, we crossed :) -
@VRonin said in Why this thread prints out nothing:
I don't think
qDebug()
is thread safe so I switched to good oldstd::cerr
This was discussed in the Development mailing list recently, but it seems to be missing from the archives. The discussion led to https://codereview.qt-project.org/#/c/208249/
Basically, qDebug is thread safe, but stderr is unbuffered. Therefore, when 2 threads write long messages to qDebug at the same time, the output from the threads could become interleaved. However, if we install a custom message handler and make qDebug write to stdout instead of stderr, the problem disappears.
-
@VRonin said in Why this thread prints out nothing:
I don't think qDebug() is thread safe so I switched to good old std::cerr
It is and has been for ages, it's just not reflected in the docs (as @JKSH pointed out). It's intended for debug only, though, not for application logging.
-
@VRonin said in Why this thread prints out nothing:
atomic_bool
using atomic_bool didn't solve the problem.
-
@joeQ not sure but this is a sample borrowed from a book about Qt.
-
- Where are you looking for the debug output?
- does it print from the main thread? (I imagine no)
-
@kshegunov from the command prompt. No it doesn't print from the main thread.
-
@CroCo
I'm interested in this, but lost as to just what your code now looks like? Is it exactly (more or less) as per your original post still, or have you done stuff as per either @joeQ or @VRonin 's code?In particular, if you sleep after you have started your threads and before you set
stopThreads = true
, you really should see output from threads'run()
?And to eliminate any arguments about
qDebug
/output, if you run under debugger and place breakpoint inTextThread::run
, what do you actually hit/not hit? (If you really can't use a debugger, at least put in aqDebug() << "Got here";
as the first statement inTextThread::run
, so we're not wondering aboutstopThreads
value.) Even though your code is "simple", at this point given that you haven't found an answer there is still quite a bit of possible simplification to reduce the issue to its nub. -
@CroCo said in Why this thread prints out nothing:
@kshegunov from the command prompt. No it doesn't print from the main thread.
Are you using Windows? Your debug output might be going to the system debug log instead of the command prompt. You can monitor the system debug log by running DebugView before you start your app: https://docs.microsoft.com/en-us/sysinternals/downloads/debugview
If you still can't see your debug messages,
- Post your *.pro file
- Post your latest code
- Tell us: How are you building your app, and how are you running your app?