为什么这个音频代码循环使用了这么多CPU/Memory?

Why is this looping of audio code using so much CPU/Memory?

void FilePlayer::setLooping(const bool newState, float startPos)
{
while (newState == true)
{
    float endPos = startPos + 4/audioTransportSource.getLengthInSeconds();

    float currentPos = audioTransportSource.getCurrentPosition()/audioTransportSource.getLengthInSeconds();

    if (currentPos == endPos || currentPos > endPos)
    {
        audioTransportSource.setPosition(startPos * audioTransportSource.getLengthInSeconds());
    }
}
}

这是循环播放任何音频文件 4 秒的代码,无论多长时间。您单击循环按钮,newState 变量变为真。我有另一个按钮将其变为 false,但是,当我在应用程序中执行循环任务时,CPU/Memory/Energy 影响达到顶峰,应用程序变得无法使用,以至于我无法单击按钮结束循环.

谁能给我解释一下这是为什么?

您是否尝试添加睡眠调用或确保您 return 到主事件循环以处理按钮点击事件?

编辑:另外,您的 newState 变量是否应该通过引用(而不是 const)传递,以便您可以更改值以停止循环?

Edit2:一个真正的 basic/simple 线程示例:

FilePlayer.h

#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>

class 
{
public:
    /**
      * @brief Constructor
      */
    FilePlayer();

    /**
     * @brief Destructor
     */
    ~FilePlayer();

private:
    /**
     * @brief startThread Starts the thread
     */
    void startThread();

    /**
     * @brief stopThread Stops the thread
     */
    void stopThread();

    /**
     * @brief theLoop The threaded loop
     */
    void theLoop(float startPos);

    std::atomic<bool> running; //lock for thread control
    std::mutex mtxAudioTransportSource; //mutex lock for audioTransportSource access
    std::thread td;   

}

FilePlayer.cpp

FilePlayer::FilePlayer()
{
    running.store(false);
}

FilePlayer::~FilePlayer()
{
    stopThreads();
}

void FilePlayer::startThreads()
{
    if(running.load()){
        //The thread is alread running, either call stopThreads before making a different one
        //or error out (I just put return in)
        return;
    }
    running.store(true);
    td = thread(theLoop, 1.0);
}

void FilePlayer::stopThreads()
{
    running.store(false);
    if(td.joinable()){
        td.join();
    }
    delete td;
    td = NULL;
}

void FilePlayer::theLoop(float startPos)
{
    while (running.load())
    {
        mtxAudioTransportSource.lock(); //Since the audioTransportSource object is being used in multiple threads, 
                                        //you must use a mutex lock to make sure only 1 thread is accessing it at any time
                                        //you will need to add the lock/unlock to all threads using it.

                                        //It is often a good idea to lock, get what you need, and unlock as soon as possible so
                                        //the other thread isn't blocked for too long if it is waiting on it.

        float audioLength = audioTransportSource.getLengthInSeconds();
        float currentPos = audioTransportSource.getCurrentPosition()/audioLength;

        mtxAudioTransportSource.unlock();

        float endPos = startPos + 4/audioLength;

        if (currentPos >= endPos)
        {
            mtxAudioTransportSource.lock();
            audioTransportSource.setPosition(startPos * audioTransportSource.getLengthInSeconds());
            mtxAudioTransportSource.unlock();
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

所以我根本没有编译它,所以可能会有一些错误,但这只是一个供您参考的示例。如果有人有任何更正或优化,请随时说出来。关于线程,要记住的最重要的事情是永远不要在不使用某种锁的情况下访问共享变量。祝你的项目好运!