简体   繁体   中英

How can I reduce the CPU usage of my application?

My application involves taking backup of certain sqlite database as set by user. This backup is based on certain conditions. So in order to do that I am using a worker object(running on QThread) do check if the conditions match. If the condition matches I take the backup. My problem is, i have checked the task manager and my application always uses a minimum of 25%(since the thread is running). If i disable the automatic backup, it is back to normal. Am I doing anything wrong or is this normal ? Please can anyone point me how to keep the cpu usage low? Below is my code for the automatic backup thread.

void Automatic_Backup_Logic::Thread_Run()
{
    QSettings settings(ORGANISATION, APPLICATION_NAME);
    while(!m_Stop){
        // First check the backup type
        int backup_type = settings.value(BACKUP_TYPE).toInt();
        QTime BackupTime = qvariant_cast<QTime>(settings.value(BACKUP_TIME));

        // Check for backup time; if it's in range only then do backup
        if(!Check_Backup_Time(BackupTime)){
            continue;
        }

        switch (backup_type)
        {
            case BACKUP_TYPE_MANUAL:
            // do nothing here
                continue;

            case BACKUP_TYPE_DAILY:
                Backup_Daily();
                break;

            case BACKUP_TYPE_WEEKLY:
                Backup_Weekly();
                break;

            case BACKUP_TYPE_MONTHLY:
                Backup_Monthly();
                break;

            case BACKUP_TYPE_YEARLY:
                Backup_Yearly();
                break;

            default:
                break;
        }

        // Wait for the current backup minute to pass by to avoid multiple copies
        QTime t1 = qvariant_cast<QTime> (settings.value(BACKUP_TIME));
        while(t1.minute()==QTime::currentTime().minute()){
            if(m_Stop)
                return;
            QCoreApplication::processEvents();
        }
    }
}

PS: I am running this thread by standard Qt Procedure that is creating a worker thread and using moveToThread() function

A clean approach to this problem would be to make use of Qt's signal/slot mechanism.

Just setup a QTimer with the remaining time to backup and connect its timeout() signal with a backup slot, like:

class Automatic_Backup_Logic {
public:
    Automatic_Backup_Logic() {
        connect(&mBackupTimer, &QTimer::timeout, this, &Automatic_Backup_Logic::performBackup);
    }

    void scheduleBackup(QTime backupTime) {
        int millisRemaining = getTimeRemaining(backupTime);
        mBackupTimer.singleShot(millisRemaining);
    }

private slots:
    void performBackup() {
    }

private:
    QTimer mBackupTimer;
}

As suggested in comments, I put delay in my thread now everything is working as expected. Thank you all

Edited Code

    // Check for backup time; if it's in range only then do backup
    while(!Check_Backup_Time(BackupTime) && !m_Stop){
        QThread::sleep(1);
        BackupTime = qvariant_cast<QTime>(settings.value(BACKUP_TIME));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM