我发现大部分服务器(例如RPG网页游戏服务器)总是自己实现定时器而不是使用系统定时器,例如在linux上,
int timerfd_create(int clockid, int flags)
Run Code Online (Sandbox Code Playgroud)
你能告诉我为什么吗?我认为系统 api(timerfd_create) 可以有更高的性能。
阅读时间(7)。确实,timerfd_create(2)是有效的,但它是 Linux 特定的。
在timer_create(2)等人中使用 Posix 计时器。可能更便携。
Linux 特定的timerfd_create(以及signalfd(2)和eventfd(2) ...)的显着优势是对事件循环友好(因为它是一个文件描述符,例如管道或套接字)。但是,大多数事件循环都建立在多路复用系统调用之上,例如poll(2)接受延迟(毫秒)或较旧的select(2)