Re: Thread safety for epoll/libaio

Anton Titov
2006-03-01T01:55:45Z

On Tue, 2006-02-28 at 15:36 -0500, Li, Peng wrote:

> Thread B: while(1) { epoll_wait(); ... }
> // same as thread A
> Thread D: ... epoll_ctl(); ....
>
> Suppose thread B calls epoll_wait and blocks before thread D calls
> epoll_ctl. Is it safe to do so? Will thread B be notified for the
> event submitted by thread D?

Hello,

I have some (more) expirience with epoll and threads and it seem to work
well. If you have epoll_wait() in one thread and another thread do
epoll_ctl to add a handle, epoll_wait will wake up as soon as the handle
is ready for operation (most of the time instantly, when operation is
write).

epoll man page states:

Q6 Will the close of an fd cause it to be removed from
all epoll sets automatically?

A6 Yes.
but I was experiencing some (rare) segfaults with my application while
benchmarking it when I just closing my descriptors. Debugging showed,
that I'm getting events for destroyed objects (with closed descriptors).
Adding epoll_ctl(..., EPOLL_CTL_DEL, ...) fixed this.