I'm building an HTTP server in C using epoll
and pthread
. The idea is to have an event loop in the main thread monitoring file descriptor and a thread pool to handle computationally-expensive operations.
What I'm struggling is that what type of operation should be done by an event loop and by a thread pool. Here is what I expect to be done:
- My epoll event structure will store a request struct and a response struct.
- After adding new file descriptors to monitor (EPOLLIN), the server will read from a file descriptor if there is a new request, and store to the request field in the epoll event
- After reading the request, the event loop will push it to a queue. A thread pool will take requests from the queue and process all the requests (assuming all static files).
- After processing, the thread will store a response back and switch the event flag to EPOLLOUT. The event loop will monitor and send out the response.
Here is what I've seen in many examples:
- Accept a new connection and add the file descriptor to the epoll interface.
- If there is an event, except from accepting new connections, the thread pool will handle data from the file descriptor.
In a nutshell, I think that threads should only handle operations such as reading large static files, and the event loop should handle network and socket operations such reading from or writing to sockets. What do you think?
(I updated this post with my own solution)