run vs movetothread ? (thread)
-
@mduzoylum
let's put it this way,
you need significantly more knowledge to correctly subclass QThread and (correctly)override run, than to simply move one object instance to an other thread -
@mduzoylum
let's put it this way,
you need significantly more knowledge to correctly subclass QThread and (correctly)override run, than to simply move one object instance to an other thread -
@mduzoylum said in run vs movetothread ? (thread):
what is the difference between run() vs movetothread() functions to use thread?
Hello, your question is very strange and it seems to me that you don't understand what QThread is...
Have you take a look at the QThread documentation?Why do you want to use threads?
Do you aware about all Qt possibilities about multi-threading?
Please read first this https://hub.packtpub.com/multithreading-qt/ to get a little in touch about what multi-thread is.Read documentation before trying to do multithread, or you will be very quickly frustrated.
my 2 cts.
-
There is also https://doc.qt.io/qt-5/threads-technologies.html
-
Is this to offend your answer ?
movetothread using for multithread, I know that.I'm using thread in jobs that need to run continuously in the background(Is the answer enough?)
Yes I'm in the beginning but your answer is irrelevant.I recommend you to speak more constructively -
@mduzoylum said in run vs movetothread ? (thread):
in jobs that need to run continuously in the background(Is the answer enough?)
I apologies if my answer is hurting you, this was not my intention.English is not my first language (I am french), so my answer is perhaps rudder as that I want to say :(
If you want to to work in background and continuously then create a worker class and assign it to a thread is, in my eyes, the right way as described in the link I give you in my previous message or those:
- http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/
- https://www.toptal.com/qt/qt-multithreading-c-plus-plus
If you just want to do a specific job in background and be informed when it is done, QtConcurrent::run() is what you need. This avoids you the create/destroy thread, you can simply call you function with specific parameters like this:
auto resultFuture = QtConcurrent::run(worker, &WorkerKlass::calculate, listOfItems);
Then you can use QFutureWatcher to be aware when calculate is finished and get the return value: