4

I am new to linux server programming with epoll. I have 2 threads: Thread_Accept and Thread_epoll. The former is block accept loop, if new connection is coming, it will add the new fd with epoll_ctl(). The latter is a big epoll_wait() loop.

Now my question is: What if when Thread_Accept executing epoll_ctl() while the Thread_epoll is in the middle of epoll_wait? Will it got any side effect?

Thanks in advance.

regards, Martin

Martin Ng
  • 89
  • 1
  • 3
  • 10

3 Answers3

7

From the epoll_wait man page:

While one thread is blocked in a call to epoll_pwait(), it is possible for another thread to add a file descriptor to the waited-upon epoll instance. If the new file descriptor becomes ready, it will cause the epoll_wait() call to unblock.

So, no side effects when adding a new file descriptor :-)

(This man page note is the result of the bug mentioned by nathansizemore)

aleixrocks
  • 198
  • 1
  • 6
  • I cannot believe this wasn't listed as FAQ #1 by the epoll author. One of the main limitations with poll and select is that they are designed for single-threaded use. Changing the monitored descriptors requires silly hoops to wake up the call like signal delivery or using a dummy pipe added to the monitored set, and then callling down again with the whole new fd set. – Kaz May 13 '20 at 13:35
0

From my experience, yes. But, this says otherwise

Community
  • 1
  • 1
nathansizemore
  • 3,028
  • 7
  • 39
  • 63
  • the link you pointed out in the "this says otherwise" actually agrees with you. You can call epoll_ctl() in thread 1, while thread 2 is epoll_wait()ing, and it'll wake up appropriately when the the fd that was epoll_ctl()ed was available. – Danny Dulai Mar 28 '16 at 17:23
  • @DannyDulai I was stating that "it will create side effects" (from my experience), but the "this says otherwise" links states that it will not. – nathansizemore Mar 29 '16 at 03:31
0

Why don't you make it one thread?

You can set the listener socket to nonblocking and add the socket to your big epoll_wait

Iwan BK
  • 44
  • 1
  • 2
  • Iwan, I know. But isn't it split to two threads will gain better performance? Just my thinking. – Martin Ng Aug 09 '11 at 07:28
  • About gaining better performance, i don't know for sure. In this case, you need to protect every epoll_ctl with locking. IMHO, it will only adding complexity to your code – Iwan BK Aug 12 '11 at 22:07