C++ how to make precise frame rate limit?

I'm trying to create a game using c++ and i want to create limit for fps but i always get more or less fps than i want. When i look at games what have fps limit it's always precise framerate. Tried using Sleep() std::this_thread::sleep_for(sleep_untill). For example Sleep(0.01-deltaTime) to get 100fps but ended up with +-90fps. How do these games handle fps so precisely wheb any sleeping isn't precise? I know i can use infinite loop that just checks if time passed but it's using full power of cpu but i want to decrease cpu usage by this limit without VSync

Yes, sleep is usually inaccurate. That is why you sleep for less than the actual time it takes to finish the frame. For example, if you need 5 more milliseconds to finish the frame, then sleep for 4 milliseconds. After the sleep, simply do a spin-lock for the rest of the frame. Something like

float TimeRemaining = NextFrameTime - GetCurrentTime();
Sleep(ConvertToMilliseconds(TimeRemaining) - 1);
while (GetCurrentTime() < NextFrameTime) {};

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) Stock analysis for Citigroup Inc (C:New York) including stock price, stock chart, company news, key statistics, fundamentals and company profile.

You may set a const fps variable to your desired frame rate, then you can update your game if the elapsed time from last update is equal or more than 1 / desired_fps. This will probably work. Example:

const /*or constexpr*/ int fps{60};

// then at update loop.
    // update the game timer.

    // check for any events.

    if(timer->ElapsedTime() >= 1 / fps)
        // do your updates and THEN renderer.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.

You could record the time point when you start, add a fixed duration to it and sleep until the calculated time point occurs at the end (or beginning) of every loop. Example:

#include <chrono>
#include <iostream>
#include <ratio>
#include <thread>

template<std::intmax_t FPS>
class frame_rater {
    frame_rater() :                 // initialize the object keeping the pace
        time_between_frames{1},     // std::ratio<1, FPS> seconds

    void sleep() {
        // add to time point
        tp += time_between_frames;

        // and sleep until that time point

    // a duration with a length of 1/FPS seconds
    std::chrono::duration<double, std::ratio<1, FPS>> time_between_frames;

    // the time point we'll add to in every loop
    std::chrono::time_point<std::chrono::steady_clock, decltype(time_between_frames)> tp;

// this should print ~10 times per second pretty accurately
int main() {
    frame_rater<10> fr; // 10 FPS
    while(true) {
        std::cout << "Hello world\n";
        fr.sleep();                   // let it sleep any time remaining

Learn C, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity

Use a timer. Some OS's can provide special functions. For example, for Windows you can use SetTimer and handle its WM_TIMER messages.

Then calculate the frequency of the timer. 100 fps means that the timer must fire an event each 0.01 seconds.

At the event handler for this timer-event you can do your rendering.

In case the rendering is slower than the desired frequency then use a syncro flag OpenGL sync and discard the timer-event if the previous rendering is not complete.

C Tutorial, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies.

C Language - Overview, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� Learn about C with our data and independent analysis including price, star rating, valuation, dividends, and financials. Start a 14-day free trial to Morningstar Premium to unlock our take on C.

Learn C Programming, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system � C - switch statement - A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is chec

C Programming Language, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. Microsoft C++, C, and Assembler documentation. Learn how to use C++, C, and assembly language to develop applications, services, and tools for your platforms and devices.

  • There is std::this_thread::sleep_until(timepoint) for that not std::this_thread::sleep_for(sleep_untill). Show your code, you sound confused.
  • Look at the source code for media players such as VLC. It's almost certainly not done with sleep() of any sort.
  • Why is this tagged "C" if it is for "C++" ?
  • In C I would use clock() (although it's not "wall time") and match the general throughput and not try to time/control an individual frame. That's good if the system can keep up. Never waste time in a game by sleeping, there must be something better to do.
  • Enable vsync to draw at the monitor's refresh rate and prevent tearing. Look into glfwSwapInterval().