zoukankan      html  css  js  c++  java
  • 如何与多个线程的操作epoll fd

           自己曾经做一个接口server时候,这样的场景下我的设计是多个线程操作同一个epoll fd。彼时,我的理由是epoll的系列函数是线程安全的。

           当然有人不理解为什么会有多个线程操作同一个epoll fd的情形,这里略微铺陈一下接口server的场景。

    epoll fd有线程1维护,监听服务端port的socket的accept出来的acceptor(即新的socket fd)也放在这个epoll fd中。当收到client链接请求时候,线程2从连接池connector pool中挑选出来一个connector。connector的作用是转发请求,此时connector会把acceptor缓存起来。假设connector收到回复后。connector会通过acceptor向client返回一些数据后。线程2此时须要把acceptor在add进epoll fd中。

          曾经我以为epoll fd是多线程安全的,我就直接通过epoll_ctl(epoll fd。acceptor。add)把acceptor放进epoll fd中。

          如今再回首看看,自己是想当然的这样操作了。没有不论什么根据。孟子曰,“行有不得,反求诸己”。既然自己无法解开困惑,那就求助伟大的man了。

    通过“man epoll_wait”后。得到这么一句话:

    NOTES
         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.
         For a discussion of what may happen if a file descriptor in an epoll instance being monitored by epoll_wait() is closed in another thread, see select(2).

           翻译后就是:假设一个线程正堵塞在epoll_pwait上,此时可能有另外一个线程要把一个socket fd加入到这个epoll fd上,假设这个这个新的socket fd被加入进去后处于ready状态,那么epoll_wait就不会再处于堵塞状态。假设由epoll fd监控的一个socket fd被另外一个线程close掉,此时系统处于何种状态请參考select(2)。通过"man 2 select"后。得到例如以下一段话:   

       Multithreaded applications
           If a file descriptor being monitored by select() is closed in another thread, the result is unspecified.  On some UNIX systems, select() unblocks and returns, with an indication that the file descriptor is ready (a subsequent I/O operation will likely fail with an error, unless another the file descriptor reopened between the time select() returned  and the I/O operations was performed).  On Linux (and some other systems), closing the file descriptor in another thread has no effect on select().  In summary, any application that relies on a particular behavior in this scenario must be considered buggy.

           翻译后。其意义为:假设一个线程中由select管理的socket被另外一个线程close掉,将会发生什么仅仅有天晓得。在一些UNIX系统中。select会结束堵塞态并返回,它会标识这个socket处于ready状态(后面对这个socket的操作会失败。os也会给出错误提示,除非在select返回和进程对这个socket进行读写这段时间段内,os又把同一个socket fd分配出去了)。在linux(和其它同类的系统)上。这样的行为不会影响select(即有堵塞态变为非堵塞态)。总之。假设一个程序中这样的行为应该被觉得是一个bug(就不应有这样的行为操作)。

           通过以上两段man大神的神示,除了一个线程在epoll或者select中监控一个socket时候另外一个线程对这个socket进行close这样的情况,我就能够觉得多个线程操作同一个epoll fd的行为是安全的。即我上面的操作是没有问题的。

           以上是个人愚见,恳请大家批评指正。

           另外严厉谴责诸如推酷“www.tuicool.com”这样的垃圾抄袭站点不经本人同意就转载我自己blog行为。

  • 相关阅读:
    一些使用Android设备调试功能的注意事项(挖职位)
    Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; neste
    HIbernate java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z
    applicationContext.xml 配置
    No setter found for property 'userDAO' in class 'com.ssh.service.impl.User1Service'
    structs 源码示例
    基于MyEclipse+9.0+++Tomcat+7.0的SSH+平台搭建
    struts2错误:The Struts dispatcher cannot be found.
    使用Struts2开发Java Web应用程序(目录)
    MyEclipse中导入Spring 4.0源码
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4593214.html
Copyright © 2011-2022 走看看