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. QThread or std::thread?
Servers for Qt installer are currently down

QThread or std::thread?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 6 Posters 12.1k Views 4 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.
  • P Offline
    P Offline
    Pippin
    wrote on 13 Oct 2015, 08:56 last edited by Pippin
    #1

    Hello Qt community,

    I was wondering which type of thread I should use for my project. The main thread would be processing the Qt loop, and the other threads would basically update variables and such (Positions, QPixmaps, etc.)

    Is there an important difference(s) I should be aware of? std::thread sounds much easier to use...

    Y 1 Reply Last reply 13 Oct 2015, 09:49
    0
    • Y Offline
      Y Offline
      yeckel
      wrote on 13 Oct 2015, 09:23 last edited by
      #2

      If you are going to communicate using signals and slots (what you should) than you need QThread.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pippin
        wrote on 13 Oct 2015, 09:32 last edited by
        #3

        Oh, is it as simple as that? Because I think I'm not going to use signals and slots.

        Y 1 Reply Last reply 13 Oct 2015, 09:39
        0
        • P Pippin
          13 Oct 2015, 09:32

          Oh, is it as simple as that? Because I think I'm not going to use signals and slots.

          Y Offline
          Y Offline
          yeckel
          wrote on 13 Oct 2015, 09:39 last edited by
          #4

          @Pippin How are you going to report to the main event loop, that the position, or pixmap changed? Or if you will use QRunnable than you are back with signal/slots. I would stick with QThread since it makes things easier and more compact. You can also start another eventloop in next thread and let computing running there. See http://thesmithfam.org/blog/2010/02/07/talking-to-qt-threads/

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pippin
            wrote on 13 Oct 2015, 09:45 last edited by
            #5

            Well everything that the other threads would be updating are QGraphicsItems that would be displayed inside a QGraphicsView. I was going to disable the automatic update of the Viewport

            myView.setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
            

            then update it whenever necessary:

            myView.update();
            

            Is it okay?

            J 1 Reply Last reply 13 Oct 2015, 11:55
            0
            • P Pippin
              13 Oct 2015, 08:56

              Hello Qt community,

              I was wondering which type of thread I should use for my project. The main thread would be processing the Qt loop, and the other threads would basically update variables and such (Positions, QPixmaps, etc.)

              Is there an important difference(s) I should be aware of? std::thread sounds much easier to use...

              Y Offline
              Y Offline
              yoavmil
              wrote on 13 Oct 2015, 09:49 last edited by
              #6

              @Pippin
              never mix Qt event loop with std::threads. it just doesn't work together.
              Qt has all the async functions you need, similar syntax to std.

              1 Reply Last reply
              0
              • P Pippin
                13 Oct 2015, 09:45

                Well everything that the other threads would be updating are QGraphicsItems that would be displayed inside a QGraphicsView. I was going to disable the automatic update of the Viewport

                myView.setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
                

                then update it whenever necessary:

                myView.update();
                

                Is it okay?

                J Online
                J Online
                JKSH
                Moderators
                wrote on 13 Oct 2015, 11:55 last edited by
                #7

                @Pippin said:

                Well everything that the other threads would be updating are QGraphicsItems that would be displayed inside a QGraphicsView. I was going to disable the automatic update of the Viewport

                myView.setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
                

                then update it whenever necessary:

                myView.update();
                

                Is it okay?

                All GUI-related classes must be used in the GUI thread only. (The GUI thread is the one which created the QApplication.) You cannot use them in other threads.

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

                P 1 Reply Last reply 13 Oct 2015, 12:49
                0
                • J JKSH
                  13 Oct 2015, 11:55

                  @Pippin said:

                  Well everything that the other threads would be updating are QGraphicsItems that would be displayed inside a QGraphicsView. I was going to disable the automatic update of the Viewport

                  myView.setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
                  

                  then update it whenever necessary:

                  myView.update();
                  

                  Is it okay?

                  All GUI-related classes must be used in the GUI thread only. (The GUI thread is the one which created the QApplication.) You cannot use them in other threads.

                  P Offline
                  P Offline
                  Pippin
                  wrote on 13 Oct 2015, 12:49 last edited by
                  #8

                  @JKSH Not sure what you're telling me: are you telling me that I must use QThreads, or are you telling me that even QThreads won't let me modify other QObjects?

                  J 1 Reply Last reply 13 Oct 2015, 13:06
                  0
                  • P Pippin
                    13 Oct 2015, 12:49

                    @JKSH Not sure what you're telling me: are you telling me that I must use QThreads, or are you telling me that even QThreads won't let me modify other QObjects?

                    J Online
                    J Online
                    JKSH
                    Moderators
                    wrote on 13 Oct 2015, 13:06 last edited by
                    #9

                    @Pippin said:

                    @JKSH Not sure what you're telling me: are you telling me that I must use QThreads, or are you telling me that even QThreads won't let me modify other QObjects?

                    I mean you cannot create QGraphicsView/GraphicsItem/QPixmap in another thread, and you cannot call those class' methods in another thread. This rule applies regardless of whether you use QThread or std::thread.

                    Anyway, the Graphics View Framework is designed to handle lots of items at the same time. Perhaps you don't need threads at all?

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

                    P 1 Reply Last reply 13 Oct 2015, 13:16
                    0
                    • J JKSH
                      13 Oct 2015, 13:06

                      @Pippin said:

                      @JKSH Not sure what you're telling me: are you telling me that I must use QThreads, or are you telling me that even QThreads won't let me modify other QObjects?

                      I mean you cannot create QGraphicsView/GraphicsItem/QPixmap in another thread, and you cannot call those class' methods in another thread. This rule applies regardless of whether you use QThread or std::thread.

                      Anyway, the Graphics View Framework is designed to handle lots of items at the same time. Perhaps you don't need threads at all?

                      P Offline
                      P Offline
                      Pippin
                      wrote on 13 Oct 2015, 13:16 last edited by Pippin
                      #10

                      @JKSH Oh wow, I wasn't aware of this. That sounds very problematic. Also, why is this not mentioned in pages like http://doc.qt.io/qt-5/qtconcurrentrun.html ?

                      The thing is, my project displays moving cards (QPixmaps) inside a QGraphicsScene/QGraphicsView instance, and I must control the FPS rate so that cards are always moving at the same speed. The (basic) idea is to slightly move 1 or more card(s), then update the QGraphicsView, then sleep, then slightly move 1 or more card(s) again, etc. at a given, fixed FPS rate so that the moving always looks natural. This is why I need another thread that computes the new positions, then update the view (so that the main thread draws it), then sleep, then repeat.

                      How can I do that?

                      J 1 Reply Last reply 13 Oct 2015, 13:45
                      0
                      • P Pippin
                        13 Oct 2015, 13:16

                        @JKSH Oh wow, I wasn't aware of this. That sounds very problematic. Also, why is this not mentioned in pages like http://doc.qt.io/qt-5/qtconcurrentrun.html ?

                        The thing is, my project displays moving cards (QPixmaps) inside a QGraphicsScene/QGraphicsView instance, and I must control the FPS rate so that cards are always moving at the same speed. The (basic) idea is to slightly move 1 or more card(s), then update the QGraphicsView, then sleep, then slightly move 1 or more card(s) again, etc. at a given, fixed FPS rate so that the moving always looks natural. This is why I need another thread that computes the new positions, then update the view (so that the main thread draws it), then sleep, then repeat.

                        How can I do that?

                        J Online
                        J Online
                        JKSH
                        Moderators
                        wrote on 13 Oct 2015, 13:45 last edited by
                        #11

                        @Pippin said:

                        I must control the FPS rate so that cards are always moving at the same speed. The (basic) idea is to slightly move 1 or more card(s), then update the QGraphicsView, then sleep, then slightly move 1 or more card(s) again, etc. at a given, fixed FPS rate so that the moving always looks natural. This is why I need another thread that computes the new positions, then update the view (so that the main thread draws it), then sleep, then repeat.

                        How can I do that?

                        Have a look at the animations framework: http://doc.qt.io/qt-5/animation-overview.html You don't need to calculate every frame yourself

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

                        P 1 Reply Last reply 13 Oct 2015, 15:57
                        0
                        • M Offline
                          M Offline
                          mcosta
                          wrote on 13 Oct 2015, 14:03 last edited by
                          #12

                          @Pippin Qt offers different solutions to use Threads. Here you can find an overview of the 4 solutions and a table that helps you to choose the best for your needs

                          Once your problem is solved don't forget to:

                          • Mark the thread as SOLVED using the Topic Tool menu
                          • Vote up the answer(s) that helped you to solve the issue

                          You can embed images using (http://imgur.com/) or (http://postimage.org/)

                          1 Reply Last reply
                          0
                          • J JKSH
                            13 Oct 2015, 13:45

                            @Pippin said:

                            I must control the FPS rate so that cards are always moving at the same speed. The (basic) idea is to slightly move 1 or more card(s), then update the QGraphicsView, then sleep, then slightly move 1 or more card(s) again, etc. at a given, fixed FPS rate so that the moving always looks natural. This is why I need another thread that computes the new positions, then update the view (so that the main thread draws it), then sleep, then repeat.

                            How can I do that?

                            Have a look at the animations framework: http://doc.qt.io/qt-5/animation-overview.html You don't need to calculate every frame yourself

                            P Offline
                            P Offline
                            Pippin
                            wrote on 13 Oct 2015, 15:57 last edited by Pippin
                            #13

                            @JKSH I've just used QtConcurrent to launch a function that makes a window visible and changes the background of a QGraphicsView, and there was no problem. I don't understand, because according to you my attempt should have failed. What am I missing?

                            Also, the following code can't seem to work:

                            	QThreadPool *pool;
                            	QFuture<void> future = QtConcurrent::run(pool, foo2);
                            

                            where foo2 is a void function without arguments. The compilation report is quite long and can be found here: http://pastebin.com/Sq35HD0v

                            I don't understand how I'm not using QtConcurrent::run() correctly. However the following code works:

                            	QFuture<void> future = QtConcurrent::run(foo2);
                            

                            If someone sees the catch, feel free to point it out. Thanks.

                            J jsulmJ 2 Replies Last reply 14 Oct 2015, 00:08
                            0
                            • P Pippin
                              13 Oct 2015, 15:57

                              @JKSH I've just used QtConcurrent to launch a function that makes a window visible and changes the background of a QGraphicsView, and there was no problem. I don't understand, because according to you my attempt should have failed. What am I missing?

                              Also, the following code can't seem to work:

                              	QThreadPool *pool;
                              	QFuture<void> future = QtConcurrent::run(pool, foo2);
                              

                              where foo2 is a void function without arguments. The compilation report is quite long and can be found here: http://pastebin.com/Sq35HD0v

                              I don't understand how I'm not using QtConcurrent::run() correctly. However the following code works:

                              	QFuture<void> future = QtConcurrent::run(foo2);
                              

                              If someone sees the catch, feel free to point it out. Thanks.

                              J Online
                              J Online
                              JKSH
                              Moderators
                              wrote on 14 Oct 2015, 00:08 last edited by JKSH
                              #14

                              @Pippin said:

                              @JKSH I've just used QtConcurrent to launch a function that makes a window visible and changes the background of a QGraphicsView, and there was no problem. I don't understand, because according to you my attempt should have failed. What am I missing?

                              The thing is, it's not guaranteed to fail immediately. Manipulating GUI classes in a different thread leads to undefined behaviour. That means, sometimes it might work as you'd expect, but it might crash at other times.

                              See the documentation: "Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread."

                              Also, the following code can't seem to work:

                              	QThreadPool *pool;
                              	QFuture<void> future = QtConcurrent::run(pool, foo2);
                              

                              where foo2 is a void function without arguments. The compilation report is quite long and can be found here: http://pastebin.com/Sq35HD0v

                              That overload of QtConcurrent::run() was introduced in Qt 5.4.0: http://code.qt.io/cgit/qt/qtbase.git/tree/dist/changes-5.4.0 (so it won't compile with older versions of Qt)

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

                              1 Reply Last reply
                              0
                              • P Pippin
                                13 Oct 2015, 15:57

                                @JKSH I've just used QtConcurrent to launch a function that makes a window visible and changes the background of a QGraphicsView, and there was no problem. I don't understand, because according to you my attempt should have failed. What am I missing?

                                Also, the following code can't seem to work:

                                	QThreadPool *pool;
                                	QFuture<void> future = QtConcurrent::run(pool, foo2);
                                

                                where foo2 is a void function without arguments. The compilation report is quite long and can be found here: http://pastebin.com/Sq35HD0v

                                I don't understand how I'm not using QtConcurrent::run() correctly. However the following code works:

                                	QFuture<void> future = QtConcurrent::run(foo2);
                                

                                If someone sees the catch, feel free to point it out. Thanks.

                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on 14 Oct 2015, 04:53 last edited by
                                #15

                                @Pippin
                                This cannot work:

                                QThreadPool *pool;
                                QFuture<void> future = QtConcurrent::run(pool, foo2);
                                

                                You did not create an instance of QThreadPool.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                P 1 Reply Last reply 14 Oct 2015, 10:15
                                0
                                • jsulmJ jsulm
                                  14 Oct 2015, 04:53

                                  @Pippin
                                  This cannot work:

                                  QThreadPool *pool;
                                  QFuture<void> future = QtConcurrent::run(pool, foo2);
                                  

                                  You did not create an instance of QThreadPool.

                                  P Offline
                                  P Offline
                                  Pippin
                                  wrote on 14 Oct 2015, 10:15 last edited by
                                  #16

                                  @jsulm Fair point, thanks.

                                  @JKSH Ok thanks. It's weird though, because I've installed Qt very recently, I would have thought that I had Qt 5.5 already.

                                  1 Reply Last reply
                                  0

                                  1/16

                                  13 Oct 2015, 08:56

                                  • Login

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