Threading
-
At present, I'm wondering about the correct way to handle multiple Threads. I've studied the example sources like mandelbrot, Queued Custom Type, wait_condition and semaphores and read the documentation about Threading. Furthermore I've found the tutorial and the discussion about "how to properly use QThreads" and now I'm totally confused. From the examples I've learned that I have to subclass QThread and reimplement the run() function. In the tutorial "how to properly use QThreads" I read that I should avoid to subclass QThread and that it is better to create a WorkerObject and move it to an thread.
I need for my application two additional threads because I have to realize a CANopen communication and therefore I need one thread for the interchange of data and another thread for the guarding functionality. Can someone explain me the right way?
-
I think that QThread is the best way, it's good for each situation and it's fast.
I usually make a subclasse of QThread but if you want to move an object to a thread it's the same.
However make a subclass of QThread is more tidy.Regards
Andrea -
You create QThread or Subclass from QThread you will be able to achieve what you want. You can create two worker threads for achieving the same.
-
Definitely nothing wrong with subclassing QThread. I do it a lot actually.
I think it's more of a "what your prefer" type of situation. I find there is more work that I have to put in with a worker/controller type threading situation (code wise) than if I just subclass.
But, this could just be habit too. Qt didn't have moveToThread() when I first played with it. :)
-
Hi,
Start by reading this page: http://qt-project.org/doc/qt-5/threads-technologies.html -- It describes all the different ways to use threads in Qt, and explains how to choose an appropriate method for your use case.
[quote author="QtTommy" date="1404222528"]now I'm totally confused. From the examples I've learned that I have to subclass QThread and reimplement the run() function. In the tutorial "how to properly use QThreads" I read that I should avoid to subclass QThread and that it is better to create a WorkerObject and move it to an thread.[/quote]There's a history behind these conflicting instructions. People used to make the same mistakes over and over again when subclassing QThread, so experienced users started recommending against it. However, the alternative method has significant downsides too.
The key is understanding how QThread works, and choosing the correct technique for your use case.
[quote author="QtTommy" date="1404222528"]I need for my application two additional threads because I have to realize a CANopen communication and therefore I need one thread for the interchange of data and another thread for the guarding functionality. Can someone explain me the right way?[/quote]The "right way" depends on your CANopen API. What does it look like? If you need to write an infinite loop to run in the other thread, subclass QThread (but don't run an event loop).
By the way, what do you mean by "guarding functionality"? What does this thread do?
-
Thanks for all the answers. Your answers are helping me a lot!
@JKSH With guarding functionality I mean the node guarding, a cyclic querying of the nodes. This is my first contact to SocketCAN and the CANopen protocoll and maybe I don't need a seperate thread for the node guarding and there is a better way. My idea is to use a seperate thread because of the short cycle time (some milliseconds) of the node guarding. -
I see.
If you need a millisecond-range loop rate, then yes it would be good to run the loop in a dedicated thread.