Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Managing a bunch of QObject-QThreads pairs?



  • Hi, I have a QObject derived subclass MasterObject that contains a bunch of QObject derived subclass WorkerObject. The WorkerObject has to perform long-running calculations and so I create a thread for each of these and use moveToThread() to move these objects to the new thread. I have to manage the number of WorkerObject during run-time and so I have created a slot in MasterObject to manage the number of WorkerObject. The problem is, I am having a hard time understanding how to properly manage the number of WorkerObject. Currently, I have the following code:

    MasterObject.h

    ...
    QList<WorkerObject*> workers;
    QList<QThread*> workerThreads;
    
    public slots:
        void setNumberOfWorkers(int num);
    

    MasterObject.cpp

    ...
    MasterObject::~MasterObject()
    {
        if(!workers.isEmpty()){
            for(int ii = 0; ii < workers.length(); ++ii){
                delete workers[ii];
                delete workerThreads[ii];
            }
           workers.clear();
           workerThreads.clear();
        }
    }
    
    void MasterObject::setNumberOfWorkers(int num)
    {
        if(workers.isEmpty()){
            for(int ii = 0; ii < num; ++ii){
                workers << new WorkerObject();
                workerThreads << new QThread(this);
                workers[ii]->moveToThread(workerThreads[ii]);
                workerThreads[ii]->start();
            }
        }
        else{
            if(num > workers.length()){
                for(int ii = workers.length(); ii < num; ++ii){
                    workers << new WorkerObject();
                    workerThreads << new QThread(this);
                    workers[ii]->moveToThread(workerThreads[ii]);
                    workerThreads[ii]->start();
                }
            }
            else if(num < workers.length()){
                for(int ii = workers.length() - 1; ii >= num; --ii){
                    delete workers[ii];
                    workers.removeLast();
                    delete workerThreads[ii];
                    workerThreads.removeLast();
                }
            }
        }
    }
    
    

    Is this the correct approach to manage the WorkerObject and it's related QThread?

    Also, the WorkerObject has just one slot calculateValues(...) which does the calculation. Let's say, I have 10 WorkerObject in 10 QThread in my MasterObject but I only need to perform 3 calculations. Then what will happen to the rest of the 7 WorkerObject? I am assuming since I am not calling their calculateValues() slot, they will be sleeping all the time. Is this the case? Will these 7 dormant WorkerObject be consuming any computing power?

    P.S. I know I can use QThreadPool and QRunnable to achieve a similar condition without having to manage the threads myself, but I want to be able to set the thread priority, so that for non-critical MasterObject and it's WorkerObject I can assign QThread::LowPriority from the beginning.


Log in to reply