TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Making an Accurate Sleep() Function

61 pointsby joemanacoalmost 3 years ago

12 comments

drewcooalmost 3 years ago
This seems like an anti-pattern.<p>I usually tell people that sleeps only belong in one place in their code and they should make linters complain if it pops up anywhere else. The place is the method they call to poll. And even then it&#x27;s better to wait for something than poll to see if we&#x27;re ready.<p>This, though, seems to be trying to sleep to slow down frame rate. What the? Maybe it&#x27;s intended as some way to make all users&#x27; experiences suck equally even if they have fast boxes. Maybe. And depending on the actual scenario maybe it makes sense to handle this with a &quot;better&quot; sleep.<p>The problem as stated is not a solved problem because it&#x27;s not generally anything anyone would want to do.<p>In fact, people might have left this intentionally hard to do to discourage its use.
评论 #31857737 未加载
评论 #31861666 未加载
评论 #31857944 未加载
评论 #31858392 未加载
评论 #31858438 未加载
评论 #31857912 未加载
评论 #31861492 未加载
kaysonalmost 3 years ago
When I was an intern at ON Semi, I worked on a testbed that was designed to stress power mosfets with extremely time-accurate pulses of current. The prototype was using an Arduino microcontroller, and I noticed that when I passed small values to delayMicroseconds(), it was just not accurate at all. After a lot of digging, it turned out that they used a small inline assembly loop to count clock cycles, but they had miscounted the number of clock cycles required by the setup. When you&#x27;re talking about 2-3us with an 8MHz clock, even half a dozen clock cycles will make a big difference!
评论 #31858655 未加载
Const-mealmost 3 years ago
It&#x27;s rarely a good idea to implement spin locks as an empty while() loop. Processors have a special instruction for the spinning, _mm_pause()
wheybagsalmost 3 years ago
On windows you can get 1ms accuracy out of the built in Sleep() function by calling timeBeginPeriod[1] It sets the resolution of timers in that process to the specified ms value. I&#x27;ve used this + spinning to make high res sleep before and it works well.<p>1: <a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;timeapi&#x2F;nf-timeapi-timebeginperiod" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;timeapi&#x2F;n...</a>
评论 #31861659 未加载
nyanpasu64almost 3 years ago
Not 100% sure the article doesn&#x27;t cover this, but if you want a constant rate of wakeups over time (not a precise duration of each sleep call), it&#x27;s important to schedule your next sleep 1 period after your <i>most recent intended wakeup time</i> (in absolute time), not 1 period after the <i>call to the sleep function</i> (in relative time), to ensure that the time spent after sleep() returns before you call sleep() again doesn&#x27;t accumulate with every sleep iteration and cause you to fall behind in real time.
sedatkalmost 3 years ago
It&#x27;s probably for the sake of an example, but writing a game&#x2F;render loop with Sleep is a bad idea, perhaps even on a realtime OS. What you should be doing is to render the frame that&#x27;s meant for the current time. This way slowdowns, hiccups, boosts don&#x27;t alter the speed of animations, and you can regulate how frequent you render regardless of the timer precision.
londons_explorealmost 3 years ago
Sleep and functions like it should be [almost] banned...<p>They choose a time that computation happens. But computation should instead be done as soon as possible, and the <i>output</i> delayed to the necessary time.<p>For example, in an animation, you don&#x27;t need to wake up the CPU every 16ms to compute the next frame. You should instead just compute all the frames and send it off to the GPU to display in order and with the right timings.
评论 #31857924 未加载
评论 #31858158 未加载
评论 #31861871 未加载
评论 #31857966 未加载
评论 #31858252 未加载
jepleralmost 3 years ago
Depending on your goals, using a deadline sleep (sleep_until in C++ parlance: <a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;thread&#x2F;sleep_until" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;thread&#x2F;sleep_until</a>) rather than a duration sleep (sleep_for) is beneficial
评论 #31861745 未加载
tyiloalmost 3 years ago
This site looks horrible for me due to some font issues?<p>See <a href="https:&#x2F;&#x2F;i.imgur.com&#x2F;dAHmjSc.png" rel="nofollow">https:&#x2F;&#x2F;i.imgur.com&#x2F;dAHmjSc.png</a>
Deukhoofdalmost 3 years ago
Linux and MinGW can just use nanosleep().<p>As for Windows, use CreateWaitableTimerEx, with a CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag.
评论 #31860732 未加载
adamnemecekalmost 3 years ago
You could also drive this using an audio queue which uses hardware to generate the timer events.
评论 #31858173 未加载
leni536almost 3 years ago
Looks like a good way to drift from the clock and eventually skip frames.