Search code examples
c++epoll

How to stop epoll_wait?


I just started coding in Linux and need to port my Win server code here using epoll. I can't figure out how to stop epoll_wait.

The epoll loop runs in separate thread(s) and the main function handles console commands. I need to stop the server after "quit" command is entered. Simply closing the master socket has no effect. So how to stop epoll correctly in this case (maybe cause somehow epoll_wait to return error in all threads)?


Solution

  • Usually, you put a special file descriptor in the epoll list of file descriptors. An eventfd or a pipe are good candidates. Whenever you need to interrupt epoll_wait, you signal that file descriptor and in the event handling loop you check that file descriptor as the loop exit criteria.