Readiness-based event queues
epoll and kqueue are known as readiness-based event queues, which means they let you know when an action is ready to be performed. An example of this is a socket that is ready to be read from.
To give an idea about how this works in practice, we can take a look at what happens when we read data from a socket using epoll/kqueue:
- We create an event queue by calling the syscall
epoll_create
orkqueue
. - We ask the OS for a file descriptor representing a network socket.
- Through another syscall, we register an interest in
Read
events on this socket. It’s important that we also inform the OS that we’ll be expecting to receive a notification when the event is ready in the event queue we created in step 1. - Next, we call
epoll_wait
orkevent
to wait for an event. This will block (suspend) the thread it’s called on. - When the event is ready, our thread is unblocked (resumed) and we return from our
wait
call with data about...