Discussion about threads, moveToThread etc.
-
[quote author="peppe" date="1311661270"]Well, do you know of a better API you can use instead of Sleep()? You could patch QThread::sleep () and submit a MR :)[/quote]
if you want to let the GUI to keep doing its while it waits then call QCoreApplication::processEvents()
if you really need to sleep because you have other code you want to run then just do it in a qthread and call moveToThread. I have done it and it works very well.
EDIT: split of of this thred: "Poor QThread::sleep() and ::usleep() resolution on Windows":http://developer.qt.nokia.com/forums/viewthread/8098/P15 , Gerolf
-
[quote author="yan bellavance" date="1311786693"]
if you want to let the GUI to keep doing its while it waits then call QCoreApplication::processEvents()
[/quote]
Doing this, no problem, all fine. Though processEvents() is some sort of "the opposite" of sleep(), I still call it during my display loop to keep the GUI responsive.[quote author="yan bellavance" date="1311786693"]
if you really need to sleep because you have other code you want to run then just do it in a qthread and call moveToThread. I have done it and it works very well.[/quote]
Doing this. But tell me: I've got a worker thread doing stuff. It waits for hardware. It needs to poll. I would like to sleep between polling, but not for 15ms, more like 0.1ms (so I don't miss when my hardware is ready). How would I do this? -
Well one thing I might have forgotten to mention is that I don't call exec in that QThread so It actually does not have an eventloop because I have implemented a polling technique. Just put a forever loop in your run function and call QThread::sleep ::msleep ::usleep from there. here is a small example:
@//the timeout periods are multiples of the delay period for consistency. I dont
//need that much accuracy but if I did I woull read a timer and do the delta
//time of each loop
void myQthread::run()
{
//do init stuff here. you could call moveTothread either in constructor of
//qthread or at the end of you init in the run function but before the forever loop.forever{ this->msleep(delay); doLogsTimeout-=delay; clockUpdTimeout-=delay; tduRefreshTimeout-=delay; if(doLogsTimeout<=0){ doLogsTimeout = LOGS_TIMEOUT_PERIOD; //....do what you need to do here } if(clockUpdTimeout<=0){ clockUpdTimeout = CLOCKUPD_TIMEOUT_PERIOD; //....do what you need to do here } if(tduRefreshTimeout<=0){ tduRefreshTimeout = TDUREFRESH_TIMEOUT_PERIOD; //....do what you need to do here } }
}
@
-
[quote author="yan bellavance" date="1311788442"]Well one thing I might have forgotten to mention is that I don't call exec in that QThread so It actually does not have an eventloop because I have implemented a polling technique. Just put a forever loop in your run function and call QThread::sleep ::msleep ::usleep from there. [/quote]
If you have read the thread completly, this is what he wants to do and what (on windows) has the 15 mSec resolution. This is not due to Qt it's due to windows. And he want's to do not busy waiting, so sleep would be great but has the resolution problem on WINDOWS.
-
[quote author="Gerolf" date="1311793338"]
If you have read the thread completly, this is what he wants to do and what (on windows) has the 15 mSec resolution. This is not due to Qt it's due to windows. And he want's to do not busy waiting, so sleep would be great but has the resolution problem on WINDOWS.[/quote]
I wrote that because i was referring to:
[quote author="peppe" date="1311757068"]
I still think that using sleep() in any code is usually wrong
[/quote]And also referring to other thread that discourage the use of sleep. I want to make evident that if moveToThread is not called then calling sleep from the QThread is actually done from the main thread and can make the GUI freeze. So I am not fixing the delay resolution issue but am trying to help him make things safer. Also I was hoping this would at least give him more constance in the sleep resolution.
-
If you don't use moveToThread, you can also call sleep inside a worker thread, that has no influence in general. Inside QThread::run you can call sleep and it will be inside the worker thread.
Call sleep in the main thread might have som unexpected result to some users as the UI freezes and mostly it is said: don't call sleep in main thread.
-
if you dont moveToThread then the worker thread and GUI thread share the same event loop and that can cause problems. For example: calling QUdpSocket polling functions such as waitForReadyRead() and waitForBytesWritten()
-
that depends where you call them. and a thread only has an event loop, if you run it. If you implement everything inside the thread's run method, and do "normal" stuff there, all works. I do that all the time, without an event loop and without moveToThread. It just depends, where you create the object. If you create them inside run, all is fine. The currently active thread during object creation is the needed information. So create the QUdpSocket inside run, it works.
EDIT: if you create the objects inside the QThread's constructor, the are attached to the creating thread, not the QTHread object
-
[quote author="yan bellavance" date="1311786486"]You have to be carefull when people tell you not to call a function because it's "dangerous". If we listen to them then we cannot call: QCoreApplication::processEvents(), QThread::sleep(), QObject::moveToThread() or others I have not read of. They say this because of the context in which they are programming, they have guidelines that limit what they can do.[/quote]
This thread has NOTHING to do with those methods. And nobody said that it's dangerous. Only that they lead to bad/fragile code.
[quote]
Of course you should never call sleep() in the GUI main thread (if you don't moveToThread() your QThread then it's still in the main thread and then I would see why calling sleep should be avoided) and if you call processEvents(), do it from the GUI main thread preferably.
[/quote]... you CAN'T call QThread::sleep from the main thread, since you don't have access to that QThread protected sleep() ...
[quote]
I built a qt app on Linux that uses all of these and it's been running for a year now without ever crashing. Also, my app often show its CPU usage as 0% in the system monitor (on windows this would be windows task manager),its very efficient. I do have an i7 intel cpu.
[/quote]And we're talking about Windows.
[quote author="yan bellavance" date="1311786693"]
[quote author="peppe" date="1311661270"]Well, do you know of a better API you can use instead of Sleep()? You could patch QThread::sleep () and submit a MR :)[/quote]if you want to let the GUI to keep doing its while it waits then call QCoreApplication::processEvents()
if you really need to sleep because you have other code you want to run then just do it in a qthread and call moveToThread. I have done it and it works very well.[/quote]
This reply is nonsense -- I was talking about QThread::sleep resolution on Windows. That is, the OP's issue.
-
[quote author="Gerolf" date="1311797834"]that depends where you call them. and a thread only has an event loop, if you run it. If you implement everything inside the thread's run method, and do "normal" stuff there, all works. I do that all the time, without an event loop and without moveToThread. It just depends, where you create the object. If you create them inside run, all is fine. The currently active thread during object creation is the needed information. So create the QUdpSocket inside run, it works.
EDIT: if you create the objects inside the QThread's constructor, the are attached to the creating thread, not the QTHread object[/quote]
if you call moveToThread and dont run exec() you do not have an event loop therefore if you have a something like a qtimer in your qthread it won't emit signals and you wont be able to trigger slots connected its signals. There actually used to be a warning about not calling waitForReadyRead and waitForBytesWritten from the main thread as it could make the app crash. I have tried it even inside a qthread and seen this happen and once i used moveToThread, it went away.
-
[quote author="yan bellavance" date="1311799380"]
if you call moveToThread and dont run exec() you do not have an event loop therefore if you have a something like a qtimer in your qthread it won't emit signals and you wont be able to trigger slots connected its signals. There actually used to be a warning about not calling waitForReadyRead and waitForBytesWritten from the main thread as it could make the app crash. I have tried it even inside a qthread and seen this happen and once i used moveToThread, it went away.[/quote]And where did you create the object? I assume in the QThread's constructor. I did not say that signal/slot work without event loop, but it totally works without moveToThread if you create the objects correctly.
"see peppes wiki article":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects
-
just tried it and it ain't working. Got a bunch of these messages:
bq. Warning: QObject: Cannot create children for a parent that is in a different thread.
Of course I can emit signals form the qthread and connect them to slots in the main thread but what you speak of requires the opposite. Anyway I think I just spammed this thread. Sorry about that but it was a discussion in which I learned something from everyone.
-
The cause of that is simple. Objects created in thread B can not have an object in thread A as their parent. So, the first object that you create in the run() method of your QThread, can not have any parent, as there is no QObject available in the same thread that can be used for that. Any following object can get the first object as parent, if that makes sense in your case.
So: you have to make sure you delete the created object yourself at an appropriate time.
-
[quote author="Andre" date="1311866425"]So: you have to make sure you delete the created object yourself at an appropriate time.[/quote]
The easiest way to achieve this is to create the first object as object on the stack, and put all other as children of it, Then everything will be deleted correctly :-)
-
/agree. Is there a way to split a thread? guess so :)
-
bq. Warning: QObject: Cannot create children for a parent that is in a different thread.
I don't know what I did yesterday trigger that message , i was trying a couple of things but I put the code back to what it was. I think it is unrelated and I have a pretty good idea where it happened. I then took a look at my run function and I did find 1 object created there who's parent is the QThread (first and only one). What I don't understand is: aren't they the same thread? The parent of the object declared in run is QThread and moveToThread has been called previously to entering run. I removed the parent but I still can't trigger slots in the qthread without an event loop.
-
I think you can boil it all down to this: Where is your object created? If it's in "QThread::run()", then it got it's own concurrent OS-based thread. If not, then it's all in Qt's main GUI thread.
Even if you create a QThread object, its signals and slots basically "live" in the main thread (as in: the thread where you created the QThread).
QThread is not so much a real OS-thread, but more like "A wrapper / container which provides a method run, which will be executed in its own thread".
So you either go the "low level road", re-implement run(), preferrably don't call exec(), and do your processing. Data exchange will have to be protected (by mutexes, semaphores, etc.).
Or, you could leave run()'s default implementation (which calls exec()). Then you call, from your main (GUI) thread, .moveToThread() on some objects. Specifically: On those objects, whose events (slots) you want to have processed by another OS thread.
So if you have an QObject MyWorker, and a slot "doWork()", and then you do:
@
MyWorker someWorker;
// now connect some signal to SLOT("doWork()") of someWorker
QThread workingThread;
someWorker.moveToThread(workingThread);
workingThread.start();
@
Then this means, whenever the doWork() slot is called, it will be executed in the context of it's own OS thread (the workingThread's thread).I personally wouldn't mix those two. EITHER don't subclass QThread at all, don't touch its constructor etc. Do everything signal-slot based, and with .moveToThread().
OR, you do implement QThread and run(), but you don't call exec() and don't do .moveToThread with this specific class. Edit: or, if you have to, call exec() from this current QThread::run() context, but don't do moveToThread on other objects.
Just my 2 cents :-)