首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EPOLLONESHOT返回多个事件

EPOLLONESHOT返回多个事件
EN

Stack Overflow用户
提问于 2012-09-10 02:33:23
回答 1查看 1.8K关注 0票数 0

目前,我正在用epoll实现一个多线程网络客户端应用程序。我的模型很简单:

  1. 获取client_fd &将请求写入远程服务器
  2. 设置fd非阻塞&将其添加到epfd(EPOLLIN_EPOLLET_EPOLLET\EPOLLONESHOT)等待响应
  3. 从fd获取EPOLLIN,读取整个响应并释放资源

我遇到的问题是,偶尔在同一个fd上得到多个EPOLLIN (通过使用EPOLLIN|EPOLLET|EPOLLONESHOT).获得)因为我已经在第一个EPOLLIN中发布了所有的资源(包括client_fd),第二个evt就毁了我的程序。

任何建议都非常感谢:)

下面是代码片段:

代码语言:javascript
复制
//multi-thread wait on the sem, since there should be only one thread 
//at epoll_wait at the same time(L-F model)
sem_wait(wait_sem); 

int nfds = epoll_wait(epoll_fd,evts,max_evt_cnt,wait_time_out);

//leader got the fds to proceed
for(int i =0; i < nfds; ++i){
    io_request* req = (io_request*)evts[i].data.ptr;
    int sockfd = req->fd;
    if(evts[i].events & EPOLLIN){
        ev.data.fd=sockfd;
        if(0!=epoll_ctl(epoll_fd,EPOLL_CTL_DEL,sockfd,&ev)){
            switch(errno){
                case EBADF:
                    //multiple EPOLLIN cause EPOLL_CTL_DEL fail
                    WARNING("delete fd failed for EBADF");
                    break;
                default:
                    WARNING("delete fd failed for %d", errno);
            }
         }
         else{
                //currently walk around by just ignore the error fd
                crt_idx.push_back(i);
         }
    }
}

if(crt_idx.size() != nfds)//just warning when the case happen
    WARNING("crt_idx.size():%u != nfds:%d there has been some error!!", crt_idx.size(), nfds);

//current leader waked up next leader, and become a follower
sem_post(wait_sem);

for(int i = 0; i < crt_idx.size(); ++i)
{
    io_request* req = (io_request*)evts[crt_idx[i]].data.ptr;
    ...do business logic...
    ...release the resources & release the client_fd
}
EN

回答 1

Stack Overflow用户

发布于 2012-09-11 18:04:18

我怀疑您的代码中有某种bug或争用条件。特别是看看你关闭插座的地方。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12344755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档