windows 为什么我会使用 Sleep() 无限超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1886527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:25  来源:igfitidea点击:

Why would I use Sleep() with infinite timeout?

windowswinapisleep

提问by sharptooth

According to MSDN, Sleep()can be provided INFINITEvalue and that "indicates that the suspension should not time out".

根据 MSDN,可以提供Sleep()INFINITE值并且“指示暂停不应超时”。

Why would I want to call Sleep() with INFINITEtimeout in my program?

为什么我要INFINITE在程序中超时调用 Sleep() ?

采纳答案by Pavel Radzivilovsky

There's no reasons one in his sane mind would ever Sleep(INFINITE). It has no practical meaning.

头脑清醒的人没有理由睡觉(无限)。它没有实际意义。

It is for generality and symmetry to WaitForSingleObject(..., timeout) and SleepEx(timeout), where INFINITE does make sense.

它是为了 WaitForSingleObject(..., timeout) 和 SleepEx(timeout) 的通用性和对称性,其中 INFINITE 确实有意义。

Reminding, that SleepEx will try to consume things out of your thread's APC queue.

提醒一下,SleepEx 将尝试消耗线程的 APC 队列中的内容。

回答by Pritesh Acharya

I have used Sleep(INFINITE) and it makes perfect sense. I've used it to keep the thread alive. I have registered for WMI notification event (ExecNotificationQueryAsync, which receives event notification infinitely) then you need to keep the application alive. dont' know if this make sense to you.

我使用过 Sleep(INFINITE) 并且它非常有意义。我用它来保持线程存活。我已经注册了 WMI 通知事件(ExecNotificationQueryAsync,它无限接收事件通知)然后你需要让应用程序保持活动状态。不知道这对你是否有意义。

回答by Steve De Caux

A sleep with no timeout does not need a timer. This reduces the overhead where you anticipate a variable-length wait but are absolutely sure that the thread will be resumed.

没有超时的睡眠不需要计时器。这减少了您预期可变长度等待但绝对确定线程将恢复的开销。

回答by Matteo Italia

As far as I know, Sleep, since they introduced SleepEx, it's just a thin, convenient wrapper around SleepEx, and when they rewrote it as a wrapper, they decided just to pass the timeout parameter to SleepEx, without processing it in any way. Obviously in this way the behavior of the function with INFINITE as timeout is propagated also to Sleep (and so must be documented), although, without the bAlertable parameter of the SleepEx, it's completely useless (a Sleep(timeout) is equal to SleepEx(timeout, FALSE), so you'll have an infinite nonalertable wait).

据我所知,Sleep,因为他们引入了 SleepEx,它只是一个围绕 SleepEx 的薄而方便的包装器,当他们将其重写为包装器时,他们决定只将超时参数传递给 SleepEx,而不以任何方式处理它。显然,通过这种方式,将 INFINITE 作为超时的函数的行为也传播到 Sleep(因此必须记录),尽管没有 SleepEx 的 bAlertable 参数,它完全没用(Sleep(timeout) 等于 SleepEx(超时,FALSE),所以你将有一个无限的非警报等待)。

On Windows CE, then, they may have decided to change this behavior because it was actually silly, so I think that a Sleep(INFINITE) on CE is translated automatically to a SuspendThread; however, on Windows they are probably forced to keep the "simple" behavior for compatibility reasons.

那么,在 Windows CE 上,他们可能决定改变这种行为,因为它实际上很愚蠢,所以我认为 CE 上的 Sleep(INFINITE) 会自动转换为 SuspendThread;但是,在 Windows 上,出于兼容性原因,它们可能被迫保持“简单”行为。

回答by Vincent

In addition to what was said (basically waiting for an interrupt to happen) You might very well have an infinite timeout without being insane. For example, I've an application (a worker) that needs to do 3 different things at a time.

除了所说的(基本上是等待中断发生)之外,您很可能会无限超时而不会发疯。例如,我有一个应用程序(一个工人)需要一次做 3 件不同的事情。

I chose to make each of those work run in new threads and have an infinite timeout in the Main() thread (as the program is not supposed to exit, except if an Exception is thrown in which case the whole app is restarted), for convenience and readability (I can comment out any of the 3 works without affecting the global behavior or easily split them to different workers if needed).

我选择让每个工作在新线程中运行,并在 Main() 线程中无限超时(因为程序不应该退出,除非抛出异常,在这种情况下整个应用程序重新启动),对于方便性和可读性(我可以在不影响全局行为的情况下注释掉 3 个作品中的任何一个,或者如果需要,可以轻松地将它们拆分给不同的工作人员)。

This probably adds a very small overhead compared to have 2 new thread + the main thread doing the 3rd work, but it's negligible considering today computers performances and memory.

与有 2 个新线程 + 主线程执行第 3 项工作相比,这可能会增加非常小的开销,但考虑到今天的计算机性能和内存,它可以忽略不计。

回答by Pavel Radzivilovsky

Well, When we need to wait until ^C but we do not want while(1);

好吧,当我们需要等到 ^C 但我们不想要 while(1);