Legacy code: why sleep before restarting a QThread that is already running?
-
In the legacy codebase that I work on, I see the following pattern twice in a wrapper function around
QThread::start(Foois derived fromQThread):void Foo::fooStart() { ... do stuff ... while (isRunning()) { // Keep wait until Qt internal flag set to false. QThread::msleep(QT_INTERNAL_FLAG_RESET_TIMEOUT); } ... do stuff ... start(); }where
const int QT_INTERNAL_FLAG_RESET_TIMEOUT = 10;So for some reason, if the thread is already running and we restart it (which according to https://doc.qt.io/qt-5/qthread.html#start would be a no-op) then for some strange reason we first wait for 10 milliseconds.
I googled for the QT_INTERNAL_FLAG_RESET_TIMEOUT or 'Qt internal flag' or 'QThread internal flag' but that didn't help me explain this.
Does anyone have an idea what is happening here? Is this some strange idiom that was required in earlier versions of Qt, or is it just unnecessary code that can be removed?
-
In the legacy codebase that I work on, I see the following pattern twice in a wrapper function around
QThread::start(Foois derived fromQThread):void Foo::fooStart() { ... do stuff ... while (isRunning()) { // Keep wait until Qt internal flag set to false. QThread::msleep(QT_INTERNAL_FLAG_RESET_TIMEOUT); } ... do stuff ... start(); }where
const int QT_INTERNAL_FLAG_RESET_TIMEOUT = 10;So for some reason, if the thread is already running and we restart it (which according to https://doc.qt.io/qt-5/qthread.html#start would be a no-op) then for some strange reason we first wait for 10 milliseconds.
I googled for the QT_INTERNAL_FLAG_RESET_TIMEOUT or 'Qt internal flag' or 'QThread internal flag' but that didn't help me explain this.
Does anyone have an idea what is happening here? Is this some strange idiom that was required in earlier versions of Qt, or is it just unnecessary code that can be removed?
@Bart_Vandewoestyne said in Legacy code: why sleep before restarting a QThread that is already running?:
Does anyone have an idea what is happening here?
As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.
-
This seems like a strange pattern. Wait until a job finishes (meanwhile blocking the caller), and then start it again. This effectively turns two threads of execution into one.
One of the QtConcurrent functions, a semaphore, or a worker thread pattern would be easier to follow and more efficient. If nothing else, QThread::wait() saves on pointless wakeups.
On the other hand, legacy codebase...
-
@Bart_Vandewoestyne said in Legacy code: why sleep before restarting a QThread that is already running?:
Does anyone have an idea what is happening here?
As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.
@jsulm said in Legacy code: why sleep before restarting a QThread that is already running?:
As far as I can see it simply waits for the thread to finish (if it is already running) before it is started again. Nothing fancy.
Oh yes... I was actually wrong when I wrote that we only wait for 10 milliseconds before restarting the thread... I must've been stilll sleeping I guess ;-) I misread the 'while' for 'if'... so you are right, it simply waits for the thread to finish (if it is already running) before starting it again.