为什么 while 循环用于非死亡进程?

发布于 2024-09-30 02:28:03 字数 417 浏览 0 评论 0原文

对于不应该死的应用程序,应该实现 while 循环,据我所知。 就像

while(){
  key = getKeyPress();
  if(key)
    processKey(key);

}

当我使用“top”时我可以看到 200 个程序挂起。这意味着有 200 个 while 循环!

我希望每个程序都能将事件挂钩到操作系统。当这些事件发生时,操作系统调用这些程序的钩子。

Web 程序在初始化时执行 javascript 代码。然后我们挂钩一个按钮 if clickes ($('button').click(myClickJs)),我们挂钩计时器 (setInterval) 等。我们不使用循环来等待页面上的整个事件。这种方法可以节省大量的CPU时间和资源。

为什么没有这样的做法呢?

For applications that shouldn't die, should implement while loop, afaik.
like

while(){
  key = getKeyPress();
  if(key)
    processKey(key);

}

I can see 200 programs hanging on when I use "top". That means there are 200 while loops!

I expect every program to put hook for the events to the operating sys. When these events occur operating sys to call these hooks of program.

Web programmes executes javascript codes at the initialization. Then we hook for a button if clickes ($('button').click(myClickJs)), we hook for timer (setInterval) etc. We dont use a loop to wait whole events on a page. Such a approach saves much cpu time and resource.

Why there isn't such approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-10-07 02:28:03

对于“不应该死的应用程序”,必须在某处实现循环。你无法回避这个问题。

在您的示例代码中,它正在等待按下某个键,然后再执行响应该按键所需执行的操作。检测到并处理按键后,如何将其重置以等待另一个按键(或其他类型的输入)?答案当然是使用循环(正如您所做的那样)。

这个“外部”循环不会以任何方式反映例程“getKeyPress()”内部发生的情况。该例程可能以两种模式运行——轮询和阻塞。

如果这是一个轮询例程,您的示例代码将有效地忙等待,直到按下一个键。众所周知,这对处理器的使用效率很低(尽管对于某些问题来说这是正确的解决方案)。

如果这是一个阻塞例程,则线程/任务将阻塞,直到按下某个键为止。当阻塞时,该线程/任务将不会消耗任何处理器周期,这将允许它们被另一个线程/任务/进程使用。就本响应而言,如何实现此阻塞调用的具体细节无关紧要。重要的概念是线程/任务可能会阻塞。

如果没有“外部”循环,无论您是否有钩子,您都只能在检测和处理任何按键时获得一次通过。

希望这有帮助。

For "applications that shouldn't die", a loop MUST be implemented somewhere. You can not get around this.

In your example code, it is waiting for a key to be pressed before doing what needs to be done in response to that key press. After the key press is detected and processed, how do you reset it to wait for another key press (or other type of input)? The answer of course is to use a loop (as you have done).

This "outer" loop does not in any way reflect upon what is going on inside the routine 'getKeyPress()'. There are two modes in which this routine might possibly operate--polling and blocking.

If this is a polling routine, your example code will effectively busy-wait until a key is pressed. This is known to be an inefficient use of the processor (though for some problems it is the right solution).

If this is a blocking routine, the thread/task will block until such time as a key is pressed. While blocked, this thread/task will not be consuming any processor cycles, which will allow them to be used by another thread/task/process. For the purposes of this response, the specific details on how this blocking call is implemented are irrelevant. The important concept is that the thread/task may block.

Without the "outer" loop, you will only get one pass at detecting and processing any key presses, whether you have hooks or not.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文