Question

In my case, the same fd (socket) is used in two epoll_wait loop (two threads), one loop is only for read (pollin), the other one is only for write (pollout), pollout is only enabled when I have data to write. Usually, when the socket is closed by peer, pollin receives first, is there any simplest way to notify write loop to trigger pollout or pollerr event (to do some clean job and remove fd from epoll)? I tried to epoll_ctl(EPOLLOUT) directly from read loop after the socket is closed by peer, but it only works on some system. I also think about to add timerfd to notify write loop.

Was it helpful?

Solution

Just close the socket fd. It will automatically deregister itself from epoll. I asked a similar question last year and got this answer:

Is it necessary to deregister a socket from epoll before closing it?

If you need to do additional cleanup on the write thread for this socket, just have your write thread also listen to the read-end of a pipe fd. Notify this thread by writing a single-byte to the pipe (but not before queuing up a message to a thread-safe message queue indicating the details of what to cleanup).

When epoll_wait returns indicating data on the pipe, read the bytes off the pipe, consume the messages on the queue, and do appropriate clean-up work.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top