Important: Please read the Qt Code of Conduct -

how autodeletation works in qthreadpool

  • "If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run() " this sentence i read from qtdocument. the autodeletion() function will delete the object of class who inheriting the QRunnable or it delete the thread allocated to it ?

  • @JadeN001 Hello, QRunnable will be deleted, and the thread will be released and become available again in the QTheadPool. You don't have to worry about the thread, that's the advantage of using QThreadPool. Just keep in mind that when using QRunnable. Only the run() method is executed in a different thread, but the QRunnable object itslef remains in the main thread, so be careful with memory access.

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

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

  • @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 to know about it.and how thread works in Qthread subclass and movetothread concept.In that two cases when thread automatically close/finished?

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

  • Moderators

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

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

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

  • Lifetime Qt Champion


    Are you trying to re-implement QThreadPool ?

  • Moderators

    @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. :)

  • @SGaist yeah

  • @ambershark okay thanks

Log in to reply