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. Creating/deleting *QThread on the stack
QtWS25 Last Chance

Creating/deleting *QThread on the stack

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadstackpointers
6 Posts 3 Posters 2.5k Views
  • 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.
  • R Offline
    R Offline
    reezeus
    wrote on last edited by
    #1

    Hi There

    I would like to implement heavy calculations in separate threads.
    At present I'm already moving all the static heavy calculations in separate threads using the moveToThread method; by static, I mean the one I know in advance, when writing the code.
    I need to implement a function that performs quite important calculations when required. I would like to avoid to hard code all the possible threads as there can be more than a 100 (not in the same time).
    What I would like to do is to be able to create instances of QThread on the stack, and delete them once the slot "quit()" of the QThread is called. So I'm thinking of a QVector<QThread *>, but not sure how to handle it.

    Can you pls shed some light on this?

    Thanks

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      Maybe the QtConcurrent namespace can help? Managing by hand such a high amount of threads might be a nightmare. Moreover having 100 threads performing heavy computation may be a performance killer. I remember somebody on the forum who implemented a neural network and used one thread per neuron. At the end having all neurons computed in one thread was faster...

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      2
      • patrikdP Offline
        patrikdP Offline
        patrikd
        wrote on last edited by
        #3

        hi reezeus,
        Have a look at QThreadpool. Helps to manage many threads in an effective way.
        best,
        patrik

        1 Reply Last reply
        3
        • R Offline
          R Offline
          reezeus
          wrote on last edited by
          #4

          I forgot QtConcurrent indeed, I use only QThread for a while now, and didn't think about it.

          Just had a look at QThreadPool and seems to be exactly what I need. I'll implement it this way and come back here with my feedback.

          Thanks.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            reezeus
            wrote on last edited by
            #5

            I've implemented QThreadPool, performance is now incredibly faster. Thanks a lot.
            Now I'm actually wondering if using QThread has a real interest. Managing QThread can be very painful sometimes (to know when the thread has finished to work, if there's still something running in it or not, delete it properly as well as the class called in the thread, etc...).
            Is it a good practice to use QThreadPool even if I have only one thread?

            JohanSoloJ 1 Reply Last reply
            0
            • R reezeus

              I've implemented QThreadPool, performance is now incredibly faster. Thanks a lot.
              Now I'm actually wondering if using QThread has a real interest. Managing QThread can be very painful sometimes (to know when the thread has finished to work, if there's still something running in it or not, delete it properly as well as the class called in the thread, etc...).
              Is it a good practice to use QThreadPool even if I have only one thread?

              JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by
              #6

              @reezeus said:

              Is it a good practice to use QThreadPool even if I have only one thread?

              I've never used QThreadPool but I can imagine that it eases scaling if you do so... The day you want to increase the number of threads, simply have a bigger pool. On the other hand if you know that one thread is what you need and will continue to need, QThread seems more reasonable IMHO.

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              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