Creating/deleting *QThread on the stack



  • 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



  • 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...



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



  • 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.



  • 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?



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.