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. how autodeletation works in qthreadpool
Qt 6.11 is out! See what's new in the release blog

how autodeletation works in qthreadpool

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 3.6k 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.
  • J JadeN001

    @Gojir4 hey thanks..

    Is it the same with Qthread ? After run() does the the thread become available when we subclass the QThread?
    What happens in movetothread regarding making the thread available again,and if so can thread be reused ?

    Gojir4G Offline
    Gojir4G Offline
    Gojir4
    wrote on last edited by
    #4

    @JadeN001 Hello. No, that's not the same. When using QThread::run() or the worker-object pattern (moveToThread), that's up to you to manage this. But usually in these cases, you creates a new thread/object every time you need to run a new one.
    Anyway what is you goal ?

    J 1 Reply Last reply
    1
    • Gojir4G Gojir4

      @JadeN001 Hello. No, that's not the same. When using QThread::run() or the worker-object pattern (moveToThread), that's up to you to manage this. But usually in these cases, you creates a new thread/object every time you need to run a new one.
      Anyway what is you goal ?

      J Offline
      J Offline
      JadeN001
      wrote on last edited by JadeN001
      #5

      @Gojir4 thanks for quick reply...actually i am implementing server of chatting application.Previously i was using MoveToThread but in that it creates thread every thime when socket is created.and i think it is bad when large number of client connected.
      so,I switched to the Threadpool.but i was confused about when the thread is free.how to know about it.and how thread works in Qthread subclass and movetothread concept.In that two cases when thread automatically close/finished?

      Gojir4G 1 Reply Last reply
      0
      • J JadeN001

        @Gojir4 thanks for quick reply...actually i am implementing server of chatting application.Previously i was using MoveToThread but in that it creates thread every thime when socket is created.and i think it is bad when large number of client connected.
        so,I switched to the Threadpool.but i was confused about when the thread is free.how to know about it.and how thread works in Qthread subclass and movetothread concept.In that two cases when thread automatically close/finished?

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #6

        @JadeN001 By threadpool, do you mean QThreadPool and QRunnable ?
        I think there is some ambiguity here. If you are refering to this post, from @ambershark, maybe that's not exactly what he meant.

        The moveToThread concept from QThread documentation is designed to destroy the Worker object when it has finished, and the thread is destroyed with the Controller class. So it depends of how you design and use it, that's up to you.

        A J 2 Replies Last reply
        1
        • Gojir4G Gojir4

          @JadeN001 By threadpool, do you mean QThreadPool and QRunnable ?
          I think there is some ambiguity here. If you are refering to this post, from @ambershark, maybe that's not exactly what he meant.

          The moveToThread concept from QThread documentation is designed to destroy the Worker object when it has finished, and the thread is destroyed with the Controller class. So it depends of how you design and use it, that's up to you.

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #7

          @Gojir4 Yea definitely not what I meant.

          @JadeN001 moveToThread does not create it's own threads. You create a thread and move a QObject onto that thread.

          So what I meant by that post was create a pool of threads (let's say 5 of them), then you can semi-randomly assign your socket connections to one of those 5 threads as they are connected. Distribute the load across all 5 threads. You will still use moveToThread you will just pass 1 of the 5 thread pointers to moveToThread when you call it.

          From the sounds of it you may want to take a step back and learn a bit about threading before you tackle something that will be this complicated. Threading is not an easy thing to understand and use properly and that will be even more of a problem in a real world application if you don't at least understand the basics before tackling something like this.

          Or if this project is something you have to move forward with right away then you can always just use a single thread for sockets. More than likely it will keep up with the work on your server. Then you can scale up to a thread pool once you understand and have things working better.

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

          J 1 Reply Last reply
          2
          • Gojir4G Gojir4

            @JadeN001 By threadpool, do you mean QThreadPool and QRunnable ?
            I think there is some ambiguity here. If you are refering to this post, from @ambershark, maybe that's not exactly what he meant.

            The moveToThread concept from QThread documentation is designed to destroy the Worker object when it has finished, and the thread is destroyed with the Controller class. So it depends of how you design and use it, that's up to you.

            J Offline
            J Offline
            JadeN001
            wrote on last edited by
            #8

            @Gojir4 yes the post you have mention is the program that i was implemented using MoveToThread concept.currently i am doing same thing using QThreadPool.and it works okay in bothcase.I am just confused about thread finish automatically or we have to.
            my general (dont have any concern with my program)Question is about when thread is free/finished in both cases

            1 Reply Last reply
            0
            • A ambershark

              @Gojir4 Yea definitely not what I meant.

              @JadeN001 moveToThread does not create it's own threads. You create a thread and move a QObject onto that thread.

              So what I meant by that post was create a pool of threads (let's say 5 of them), then you can semi-randomly assign your socket connections to one of those 5 threads as they are connected. Distribute the load across all 5 threads. You will still use moveToThread you will just pass 1 of the 5 thread pointers to moveToThread when you call it.

              From the sounds of it you may want to take a step back and learn a bit about threading before you tackle something that will be this complicated. Threading is not an easy thing to understand and use properly and that will be even more of a problem in a real world application if you don't at least understand the basics before tackling something like this.

              Or if this project is something you have to move forward with right away then you can always just use a single thread for sockets. More than likely it will keep up with the work on your server. Then you can scale up to a thread pool once you understand and have things working better.

              J Offline
              J Offline
              JadeN001
              wrote on last edited by
              #9

              @ambershark yeah thanks it was my misunderstanding..
              how can i identify when moveToThread used in threadpool(5 threads) which thread is free and when?
              i have to check firstly which thread is free form pool so that i can pass that thread pointer to moveToThread

              A 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                Hi,

                Are you trying to re-implement QThreadPool ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                J 1 Reply Last reply
                2
                • J JadeN001

                  @ambershark yeah thanks it was my misunderstanding..
                  how can i identify when moveToThread used in threadpool(5 threads) which thread is free and when?
                  i have to check firstly which thread is free form pool so that i can pass that thread pointer to moveToThread

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #11

                  @JadeN001 I would just use a QThreadPool like @SGaist mentioned. Then you don't need to worry about thread assignment. Nice and easy. That's why I love Qt, it does all the hard stuff for you. :)

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

                  J 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    Are you trying to re-implement QThreadPool ?

                    J Offline
                    J Offline
                    JadeN001
                    wrote on last edited by
                    #12

                    @SGaist yeah

                    1 Reply Last reply
                    0
                    • A ambershark

                      @JadeN001 I would just use a QThreadPool like @SGaist mentioned. Then you don't need to worry about thread assignment. Nice and easy. That's why I love Qt, it does all the hard stuff for you. :)

                      J Offline
                      J Offline
                      JadeN001
                      wrote on last edited by
                      #13

                      @ambershark okay thanks

                      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