Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:57:45+00:00 2026-05-21T06:57:45+00:00

A simple game loop runs somewhat like this: MSG msg; while (running){ if (PeekMessage(&msg,

  • 0

A simple game loop runs somewhat like this:

MSG msg;
while (running){
    if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
        try{
            onIdle();
        }
        catch(std::exception& e){
            onError(e.what());
            close();
        }
}

(taken from this question)

I’m using windows as an example here for the sake of the example, it could be any platform. Correct me if I’m mistaken, but such a loop would use 100% of a cpu/core (uses 50% of one core on my computer) since it’s always checking for the state of the running variable.

My question is, would it be better (performance-wise) to implement the game loop using the OS’ (in the example Windows) timer functions, setting the required interval according to the desired number of ticks of game logic wanted per second? I ask this because I assume the timer functions use the CPU’s RTC interrupts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:57:45+00:00Added an answer on May 21, 2026 at 6:57 am

    Typically, a game will keep drawing new frames all the time even when the user does not do anything. This would normally happen where you have your onIdle() call. If your game only updates the window/screen when the user presses a button or such, or sporadically in between, then MSGWaitForMultipleObjects is a good option.

    However, in a continuous-animation game, you would normally not want to block or sleep in the render thread at all if you can help it, instead you want to render at maximum speed and rely on vertical sync as a throttle. The reason for that is that timing, blocking, and sleeping is unprecise at best, and unreliable at worst, and it will quite possibly add disturbing artefacts to your animations.
    What you normally want to do is push everything belonging to a frame to the graphics API (most likely OpenGL since you said “any platform”) as fast as you can, signal a worker thread to start doing the game logic and physics etc, and then block on SwapBuffers.

    All timers, timeouts, and sleep are limited by the scheduler’s resolution, which is 15ms under Windows (can be set to 1ms using timeBeginPeriod). At 60fps, a frame is 16.666ms, so blocking for 15ms is catastrophic, but even 1ms is still a considerable time. There is nothing you can do to get a better resolution (this is considerably better under Linux).

    Sleep, or any blocking function that has a timeout, guarantees that your process sleeps for at least as long as you asked for (in fact, on Posix systems, it may sleep less if an interrupt occurred). It does not give you a guarantee that your thread will run as soon as the time is up, or any other guarantees.
    Sleep(0) under Windows is even worse. The documentation says “the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately”. Reality has it that it works kind of ok most of the time, blocking anywhere from “not at all” to 5-10ms, but on occasions I’ve seen Sleep(0) block for 100ms too, which is a desaster when it happens.

    Using QueryPerformanceCounter is asking for trouble — just don’t do it. On some systems (Windows 7 with a recent CPU) it will work just fine because it uses a reliable HPET, but on many other systems you will see all kinds of strange “time travel” or “reverse time” effects which nobody can explain or debug. That is because on those systems, the result of QPC reads the TSC counter which depends on the CPU’s frequency. CPU frequency is not constant, and TSC values on multicore/multiprocessor systems need not be consistent.

    Then there is the synchronization issue. Two separate timers, even when ticking at the same frequency, will necessarily become unsynchronized (because there is no such thing as “same”). There is a timer in your graphics card doing the vertical sync already. Using this one for synchronisation will mean everything will work, using a different one will mean things will eventually be out of sync. Being out of sync may have no effect at all, may draw a half finished frame, or may block for one full frame, or whatever.
    While missing a frame is usually not that much of a big deal (if it’s just one and happens rarely), in this case it is something you can totally avoid in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple way to make a game loop in JavaScript? something like...
Following this : Best approach for oldschool 2D zelda-like game I got a simple
I'm programming a simple text-based RPG using a switch statement for a game loop.
I'm trying to develop a simple text-based hangman game, and the main game loop
I have a simple iPhone game consisting of two threads: the main game loop
I am programming a simple 2d game engine. I've decided how I'd like the
My question involves a simple game that 2-4 people can play. This is a
I am creating a simple Sudoku game. Since this is my first big i
I've cobbled together a simple game loop, mostly using the techniques that seem be
I'm creating a simple 2D game in java. I've only done this in C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.