The Elusive Cross-Platform Timer API: A Journey Through OS APIs
This article explores the challenges of cross-platform timer APIs in C programming. The author discovers that different Unix systems (including Linux, FreeBSD, NetBSD, etc.) handle timers very differently. The POSIX timer_create function, based on signals, presents numerous problems, such as poor interoperability with other OS primitives and signal mask interference. The article delves into the pros and cons of various solutions, including timerfd_create, kqueue, port_create, and io_uring, ultimately concluding that for cross-platform applications, implementing timers in userspace, as libuv does, is a more efficient and reliable approach. Libuv uses a min-heap data structure to manage timers and uses system calls like poll/epoll/kqueue for multiplexing.