Frame rate control

I am creating a bunch of threads that should do the work in a frame loop. I would like to control how many frames are performed per second. I have simplified the code that I have, so I can show you what I wrote

// setup the frame timer
std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();

while(running == true)
{
    // update timer
    start = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = start - end;
    double frameTime = elapsed_seconds.count();

    this->Work(frameTime);

    // update timer
    std::chrono::time_point<std::chrono::system_clock> afterWork = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsedWorkTime = afterWork - end ;

    const double minWorkTime = 1000 / this->timer.NumberOfFramePerSeconds;
    if(elapsedWorkTime.count() < minWorkTime)
    {
        double timeToSleep = minWorkTime - elapsedWorkTime.count();
        std::this_thread::sleep_for(std::chrono::milliseconds((int)timeToSleep));
    }

    // update fps
    end = start;
    timer.FrameCount += 1;
}

Not all threads have the same amount of work, some of them have more than others, and therefore without sleep I get results that are around this Thread 1 : 150fps, Thread 2: 5000fps, Thread 3: 5000fps, Thread 4: 5000fps

What I want is to set the number of frames per second to 60, and therefore, using the code above, I would set this->timer.NumberOfFramePerSecondsto 60. My problem is that when I do this, I get a result like thisThread 1 : 30fps, Thread 2: 60fps, Thread 3: 60fps, Thread 4: 60fps

, - , 1 150 , , , 60, .

/​​?

+4
3

, , , . chrono . :

double frameTime = elapsed_seconds.count();
this->Work(frameTime);

Work, duration<double> double? , double , Work. - , :

this->Work(elapsed_seconds.count());

:

std::chrono::duration<double> elapsedWorkTime = afterWork - end ;

const double minWorkTime = 1000 / this->timer.NumberOfFramePerSeconds;
if(elapsedWorkTime.count() < minWorkTime)

elapsedWorkTime . minWorkTime . if . :

std::chrono::duration<double> minWorkTime(1./this->timer.NumberOfFramePerSeconds);
if(elapsedWorkTime < minWorkTime)

, minWorkTime ( ):

std::chrono::duration<double, std::milli> minWorkTime(1000./this->timer.NumberOfFramePerSeconds);
if(elapsedWorkTime < minWorkTime)

, 1000 - , .

    double timeToSleep = minWorkTime - elapsedWorkTime.count();
    std::this_thread::sleep_for(std::chrono::milliseconds((int)timeToSleep));

chrono . :

    std::this_thread::sleep_for(minWorkTime - elapsedWorkTime);

, :

    auto timeToSleep = minWorkTime - elapsedWorkTime;
    std::this_thread::sleep_for(timeToSleep);

.count() . duration s. , chrono. .count(), . chrono, , .count(), chrono .

VS2013 , . , ...

std::this_thread::sleep_for(std::chrono::duration_cast
                <std::chrono::milliseconds>(minWorkTime - elapsedWorkTime));

, chrono.

Caesar .

+4

: . . SO.

, , , , .

. - , , .

+2

, .

, Windows 15,6 . 1 , 15,6 , 1 . , 6.8 , . , , .

, , . ( , 60 , , - , , .)

, , , , . , 3.2 * clock_resolution, 3 * clock_resolutions .

, . , , , .

+1

All Articles