TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The missing cross-platform OS API for timers

49 点作者 broken_broken_3 个月前

8 条评论

hkwerf3 个月前
This article fails to separate the concerns one has with timers. There&#x27;s three cases I see:<p>The simple case is just putting a thread to sleep for a given time. It&#x27;s somewhat odd that there&#x27;s no portable API for that, granted. At least POSIX has sleep, nanosleep and clock_nanosleep (list edited, thanks oguz-ismail).<p>The second case is that a thread wants to continue processing until a timer has passed. This has to happen cooperatively one way or another, so setting a timer is equivalent to regularly querying the system time and comparing to a limit. There is simply no need for a &quot;timer&quot; operating system facility. (Even if you were to plug POSIX timers in here, you&#x27;d in all likelihood still need to check some flag set in the signal handler in the processing thread. And then you&#x27;d still need to check the system time as signals can originate from anywhere, just as with sleep&#x2F;etc. above.)<p>In the third case, a thread waits for I&#x2F;O. Then, of course, the call to that I&#x2F;O facility defines how any timer is handled. As these facilities are not portable, timers aren&#x27;t portable. The author refers to this realization as the need to implement &quot;Userspace timers&quot;. These are just an artifact of the I&#x2F;O facility and calls querying the system time, though.<p>So, ultimately, not having portable timers is the least of our problems. So long as we don&#x27;t unify select, poll, kqueue, io_uring and what not, we don&#x27;t have a need for them. And, as the author realized, libraries like libuv, which do an acceptable job at unifying those facilities, tend to provide a portable timer API.
评论 #42962050 未加载
评论 #42964909 未加载
评论 #42961965 未加载
评论 #42961960 未加载
评论 #42962193 未加载
tom_3 个月前
For Windows, you can use CreateWaitableTimer to create a timer that doesn&#x27;t need a window: <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;synchapi&#x2F;nf-synchapi-createwaitabletimerw" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;synchapi...</a><p>Wait for it to time out using one of the WaitFor family of functions.
评论 #42965640 未加载
pjc503 个月前
Very minor point: Windows applications only come with a message queue if you explicitly start one, so if you have a headless application it needs to have an invisible &quot;window&quot;. However I agree with the author that it <i>is</i> a pretty nice API in general. There are some problems caused by the very limited parameter size, but it&#x27;s a standard notification API for asynchronous OS events. We use it headlessly for detecting USB insertions.
评论 #42963799 未加载
评论 #42967659 未加载
gpderetta3 个月前
I find that you already have an event loop built on top of poll&#x2F;epoll&#x2F;io_uring, maintaining a timer heap (or wheel) in userspace and setting the polling timeout to the next expiration is the easiest and more efficient solution. Use an eventfd to force an early wakeup if you modify timers concurrently with your waits.<p>The most annoying thing is that epoll supports timeouts smaller than a millisecond only since 5.11.<p>edit: that&#x27;s literally what&#x27;s described under &quot;All OSes: timers fully implemented in userspace &quot;, including the comment about epoll low resolution.
mrpippy3 个月前
macOS does support EVFILT_TIMER, unfortunately the man pages that Apple hosts are very out-of-date. A more recent page (<a href="https:&#x2F;&#x2F;www.manpagez.com&#x2F;man&#x2F;2&#x2F;kqueue&#x2F;osx-10.13.1.php" rel="nofollow">https:&#x2F;&#x2F;www.manpagez.com&#x2F;man&#x2F;2&#x2F;kqueue&#x2F;osx-10.13.1.php</a>) shows support, and it&#x27;s been there since at least 10.9 (2013).
评论 #42967731 未加载
devit3 个月前
The article&#x27;s conclusion is completely wrong because it misses two crucial points:<p>1. On multicore machines you want to process timers in parallel on multiple cores. With userspace timers you either set the same timeouts on all threads and have unnecessary wakeups or distribute timers to cores ahead of time which leads to increased latency if a thread is stalled for any reason. I think this is unfixable without a dedicated timer API.<p>2. Good timer APIs let you set a time _interval_ for when the timer expires, which is essential so that the system can group timers and reduce wakeups (i.e. you process all timers where the lower bound has been reached before going to sleep, but don&#x27;t wake up until the upper bound arrives). Most or all &quot;wait with timeout&quot; APIs only have a single timeout, although this could be fixed.
评论 #42962879 未加载
thijsvandien3 个月前
Windows offers several kinds of timers. For example, there&#x27;s also <i>CreateTimerQueueTimer</i>, which (in contrast to <i>SetTimer</i>) does not require a message loop.
Veliladon3 个月前
I miss having an NMI that you got at 50&#x2F;60Hz come hell or high water.
评论 #42966909 未加载
评论 #42966631 未加载
评论 #42964991 未加载
评论 #42964179 未加载