Search code examples
c++multithreadingepoll

How to wake up epoll_wait() before any event happened?


I have a Connection object that represents a connection managed by epoll. On events, the Connection::eventWrite() or Connection::eventRead() are invoked. Connection is persistent (exists always).

I need to send data via that Connection from another thread, but don't want to access the object Connection directly, because I dont want to solve multithreading issues inside the Connection object (Connection is accessed from epoll-thread and from "sending thread"). Instead, I want to tell epoll-thread: "wake up and do something with Connection" --epoll_wait() should return and my code will find out that a CUSTOM EVENT is occured (and, for example, some function should be called, where the desired access to Connection will be performed (assign sending data to object, calling writeEvent()), then epoll_wait()-loop will be continued.

What kind of solutions exists for waking up a epoll_wait() and which one is faster? For now, I came up with creating a pipe (handled by the same epoll) and writing a byte 1 to it. What else IPC exists which can be expressed via int "epollable" file descriptor?


Solution

  • Epoll can wait on eventfd. In your case, I'd create an eventfd and than trigger this event. I think, this is the cleanest solution.