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. QtConcurrent vs QThreadPool vs QThread. Which to use?
Forum Updated to NodeBB v4.3 + New Features

QtConcurrent vs QThreadPool vs QThread. Which to use?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 6 Posters 5.7k Views 2 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello!
    The global idea is to keep object in the thread and provide slots to QML. Which is better to use QtConcurrent or QThreadPool or QThread? Is there any inbox solution for it in QT?
    I need something like this:

    KroMignonK B 2 Replies Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Docs

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • B bogong

        Hello!
        The global idea is to keep object in the thread and provide slots to QML. Which is better to use QtConcurrent or QThreadPool or QThread? Is there any inbox solution for it in QT?
        I need something like this:

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @bogong said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

        The global idea is to keep object in the thread and provide slots to QML. Which is better to use QtConcurrent or QThreadPool or QThread? Is there any inbox solution for it in QT?

        I don't understand your problem.
        Why not simply use Q_PROPERTY?

        • https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        VRoninV 1 Reply Last reply
        0
        • KroMignonK KroMignon

          @bogong said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

          The global idea is to keep object in the thread and provide slots to QML. Which is better to use QtConcurrent or QThreadPool or QThread? Is there any inbox solution for it in QT?

          I don't understand your problem.
          Why not simply use Q_PROPERTY?

          • https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @KroMignon said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

          Why not simply use Q_PROPERTY?

          Qt Quick will invoke the methods to read/write the property directly (i.e. from the GUI thread) so it would be a race condition if the objects lived on a different thread unless, of course, you manually mutex the getters/setters on the C++ side

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          KroMignonK 1 Reply Last reply
          0
          • VRoninV VRonin

            @KroMignon said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

            Why not simply use Q_PROPERTY?

            Qt Quick will invoke the methods to read/write the property directly (i.e. from the GUI thread) so it would be a race condition if the objects lived on a different thread unless, of course, you manually mutex the getters/setters on the C++ side

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @VRonin said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

            Qt Quick will invoke the methods to read/write the property directly (i.e. from the GUI thread) so it would be a race condition

            Yes, but that is up to the developper to handle this, you will always have to use mutex/atomics if you want to use multi-threading.
            QtConcurrent/QThreadPool/QThread will not solve this!

            The easiest way, IMHO, is to use Q_PROPERTY with signals to propagate the changes.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • B bogong

              Hello!
              The global idea is to keep object in the thread and provide slots to QML. Which is better to use QtConcurrent or QThreadPool or QThread? Is there any inbox solution for it in QT?
              I need something like this:

              B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              @bogong said in QtConcurrent vs QThreadPool vs QThread. Which to use?:

              keep object in the thread

              I think QtConcurrent or QThreadPool doesn't act like that.

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                I believe that all three possibilities have different use cases. QtConcurrent and QThreadPool are more task-based whereas QThread is, well, a thread.

                QtConcurrent provides easy access to put single functions into a multithreaded environment. I think it even uses a thread pool in the background. QtConcurrent is especially helpful in the context of the map and reduce operations. With QtConcurrent::run() the idea is just asynchronous execution. If you have short tasks you can use QtConcurrent.

                The QThreadPool has two advantages over plain old threads: First, you don't have the thread creation cost if you spawn many short lived threads. And second, it helps avoiding oversubscription of CPU cores. In that sense its use is similar to QtConcurrent::run(): it is best utilized for short lived tasks.

                If you want to a have a thread permanently running, QThread is your only reasonable choice. My preference is to use the default implementation of a thread and have it run the event queue. Then, I can use QMetaObject::invokeMethod with the thread as its first parameter to queue some work into that thread.

                That all said: You should think twice if you really want to go down this route. First of all, it is a hassle to manage the GUI correctly from a separate thread as all GUI calls need to be inside the GUI thread. If you don't have a performance problem with your software, don't use additional threads. It has a non-negligible performance overhead. This is fact might introduce performance problems on its own. Furthermore, multithreading is prone to additional errors and bugs. This last argument is why you should avoid threads if they are not necessary for your performance.

                1 Reply Last reply
                4

                • Login

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