Discussion about threads, moveToThread etc.
-
Hi Ak@sh, see http://qt-project.org/doc/qt-5.1/qtcore/qthread.html
By the way, this thread is 2 years old. It's best to start a new thread next time.
-
Hi Ak@sh,
you can find answer of your question in the documentation.
-
[quote author="Ak@sh" date="1374741806"]Hi JKSH,
Tnq very much for your reply.
I knew these tow ways but I realy confused when and where I should used which way(overwrite the run() or moveToThread()).[/quote]Hi Ak@sh, you're welcome.In general, I would:
- Use moveToThread() if I want two-way communication with the thread using signals+slots or events.
- Reimplement run() if my thread is very simple, and I don't need to send signals/events to the thread..
If you tell us what you plan to do with your threads, we can give you better advice.
-
Hi JKSH,
thanx for your reply.
In my current projct I already used overwrite the run() method for thread.But I confused when I saw moveToThread() Beacuse,
Some experts says overwrite the run() is not a proper way to thread.please once see this link:-
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/I request you to reply on this.
Thanx in advance…..
-
Hi, the blog goes from one extreme to the other.
In fact, both are right usages. The documentation will be useful for you http://qt-project.org/doc/qt-5.1/qtcore/thread-basics.html
BTW, if you are interested in the topic, you can have a look at
http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html
-
Nice Documenaion and han for that.
They mention this two points -
If you do not really need an _ -event loop-_ in the thread, you should subclass.
If you need an -event loop- and handle signals and slots within the thread, you may not need to subclass.What they exactly mean with EVENT LOOP ???
I know might this is very silly que for you but please.... -
An event loop is an infinite loop that processes signals and events. It waits for signals/events to arrive, then it calls functions to handle them.
The main thread starts an event loop by calling QCoreApplication::exec():
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// ...// This line starts an (infinite) event loop, then // returns its status when the loop is exited return a.exec();
}
@QThread starts an event loop by calling QThread::exec():
@
void QThread::run()
{
// This line starts an (infinite) event loop, but
// doesn't return an exit status value
this->exec();
}
@Also, see "this page":http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html
-
Hi,
At first, I don't think other can make you understand this in short word, if the documentation can not help you.
But I think you can easily find with method is suitable when you writing your application.
- Subclass the QThread and re-implement the QThread::run()
- If you find that slots doesn't work correctly without moveToThread(this), then you are doing it wrong. Please use another method.
3 Otherwise, you are doing it right.
[quote author="Ak@sh" date="1375074051"]Hii..gm.
Thnx for ur important time and reply on my query.
I am not relates the things.
come again on our main point and please comfirm me that then when we you should subclass QThread class for thread then there is no EVENT LOOP?[/quote] -
[quote author="Ak@sh" date="1375085561"]please can you give me one simple example or senario which shows clearly need of moveToThread(this) over subclass QThread...[/quote]It's easier to do it the other way round. Please tell us what you want to do, and we'll explain which method you need.
The experts told people to stop subclassing QThread because many people did it wrongly. It is easier to make mistakes when subclassing QThread. But, you are still allowed to subclass QThread.
-
In my project from one hardware I have to receive data from serial and acording to updated data I have to update my GUI.Paralally user can perform some operations on GUI(like delete/add/update etc...).
So In above my requirement 3 threads will work -
- main thread i.e. GUI
2.thread which send request to H/w
3.thread which continuously waiting for data to receive from H/w.
- main thread i.e. GUI
-
-
It sounds like you want to send requests when a user clicks on something in the GUI? If so, this thread needs to receive signals from your GUI so it needs an event loop. Use moveToThread() to put a worker QObject in the thread to handle the signals.
-
How do you receive data? The answer will determine if you need an event loop or not. (Classes like QTcpSocket and QSerialPort need an event loop to work. But if you use low-level code to poll your hardware, then you don't need an event loop)
-
-
Hi KJSH.....
thnx for your reply.
from serial line I have to recive data.
-Actualy I read data continuously from serial line and then put into system queue.
-and my next thread available to read system queue continuously so whatever data receive other thread can use it to update on GUI.And all this I implemented by subclass QThread and override run() and all working well.
-
If it is already working well, then don't change it :)
Usually, people discuss about "how should I implement ________?" before starting the project. If you need threads in the future, just remember: there are 2 ways to use QThread, and the "best" method depends on what you want to do.
- If you need to receive signals/events in the other thread, then don't subclass QThread.
- If you don't need receive signals/events in the other thread, then subclass QThread.
-
yes I agree with you but What I implemented is wrong then according to your two ways:
- If you need to receive signals/events in the other thread, then don’t subclass QThread.
- If you don’t need receive signals/events in the other thread, then subclass QThread.
Because I emits signals from my Thread which receiving data from Serial lines and those signals handled by other(GUI) thread.
and I subclass QThread in my implementation and now I feel its wrong implementation according to you. -
[quote author="Ak@sh" date="1375331989"]
- If you need to receive signals/events in the other thread, then don’t subclass QThread.
- If you don’t need receive signals/events in the other thread, then subclass QThread.
Because I emits signals from my Thread which receiving data from Serial lines and those signals handled by other(GUI) thread.
and I subclass QThread in my implementation and now I feel its wrong implementation according to you.[/quote]Don't worry, it is not wrong.I said, if a thread receives signals, then that thread should not use a subclassed QThread. But, if the thread only emits signals (not receive), you can subclass QThread.
A thread needs an event loop to receive signals, but it doesn't need an event loop to emit signals.