What is a busy loop?

后端 未结 3 1752
粉色の甜心
粉色の甜心 2020-12-21 16:41

Can it be a loop with or without statements?

while (1)
{
   //Empty
}

OR

int i = 0;

while (1)
{
   i++;
}
相关标签:
3条回答
  • 2020-12-21 17:21

    I'm pretty sure you're talking about busy wait where you have a loop polling for some condition every iteration.

    0 讨论(0)
  • 2020-12-21 17:36

    To me, a busy loop is a loop that never blocks.

    Blocking is a behavior provided by the operating system that allows a thread to consume NO cpu cycles until some condition is met (a condition variable is signaled, or perhaps just data arriving on a socket (as recv() will block)).

    In a traditional Win32 main loop, you potentially block every time your thread calls GetMessage(). All event driven windowing system are similar in this way.

    0 讨论(0)
  • 2020-12-21 17:43

    A "busy loop" or more commonly "busy wait" is an active polling where the application is waiting on some event to occur and continuously checks for it. Typically this includes a timed sleep or other task which gives up CPU time so that another process can provide the expected input.

    Contrast this with a callback. While waiting on a callback, the program consumes no CPU cycles. Typically the program will "register" a callback routine which some monitoring application invokes based on some event.

    The distinction is that the program in a busy-loop consumes CPU and time slices while waiting while the callback mechanism allows a program to consume no (or almost no) CPU while waiting.

    0 讨论(0)
提交回复
热议问题