简体   繁体   English

epoll 线程安全吗?

[英]Is epoll thread-safe?

There are two functions in epoll: epoll中有两个功能:

  1. epoll_ctl epoll_ctl
  2. epoll_wait epoll_wait

Are they thread-safe when I use the same epoll_fd?当我使用相同的 epoll_fd 时它们是线程安全的吗?
What will happen if one thread calls epoll_wait and others call epoll_ctl at the same time?如果一个线程调用 epoll_wait 而其他线程同时调用 epoll_ctl 会发生什么?

It is thread-safe, but there isn't much documentation that explicitly states that.它是线程安全的,但没有太多文档明确说明这一点。 See here这里

BTW, you can also have multiple threads waiting on a single epoll_fd , but in that case it can get a bit tricky.顺便说一句,您也可以让多个线程在单个epoll_fd上等待,但在这种情况下,它会变得有点棘手。 (Ie you might want to use edge-triggered EPOLLET or oneshot mode EPOLLONESHOT . See here .) (即,您可能想使用边缘触发的EPOLLET或 oneshot 模式EPOLLONESHOT 。请参见此处。)

Note: The existing accepted answer is correct, but it mentions " there isn't much documentation that explicitly states that ", while epoll documentation does state it.注意:现有接受的答案是正确的,但它提到“没有多少文档明确指出”,而 epoll 文档确实 state 。

The manual page for epoll_wait explicitly allows adding a file descriptor to an epoll set while it is being waited for in another thread: epoll_wait手册页明确允许在另一个线程中等待时将文件描述符添加到 epoll 集中:

Section "Notes": “注释”部分:

While one thread is blocked in a call to epoll_wait() , it is possible for another thread to add a file descriptor to the waited-upon epoll instance.当一个线程在对epoll_wait()的调用中被阻塞时,另一个线程可以将文件描述符添加到等待的epoll实例中。 If the new file descriptor becomes ready, it will cause the epoll_wait() call to unblock.如果新的文件描述符准备就绪,它将导致epoll_wait()调用解除阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM