Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Threading
Forum Updated to NodeBB v4.3 + New Features

Threading

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtTommy
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andrea993.93
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          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. :)

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            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?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QtTommy
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                I see.

                If you need a millisecond-range loop rate, then yes it would be good to run the loop in a dedicated thread.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved