野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11196|回复: 0

[其它] 【求助】epoll_wait阻塞

[复制链接]
发表于 2020-3-16 16:14:32 | 显示全部楼层 |阅读模式
本帖最后由 yukang1744 于 2020-3-16 16:19 编辑

#include <stdio.h>
#include <time.h>
#include <unistd.h>  // usleep 需要
#include <sys/epoll.h> /* epoll function */
#include <pthread.h>
#define MAX_EPOLL_SIZE 100
#define MAXLINE 10240

int   epoll_fd;
struct epoll_event ev1;
int listenfd1 = 1;

void *thread_func1(void *arg)
{        
               sleep(1);
                //ev1.events = EPOLLIN | EPOLLET;
              //ev1.events = EPOLLIN;
                ev1.data.fd = listenfd1;
                printf("thread_func1 start\n");

                if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listenfd1, &ev1) < 0)
                    fprintf(stderr, "epoll set insertion error: fd=%d\n", listenfd1);     

       printf("thread_func1 end\n");
       pthread_exit(NULL);
}


int main()
{
     pthread_t _mthread1;
     int n;
     int listenfd = 1, nfds;
     struct epoll_event ev;
     struct epoll_event events[MAX_EPOLL_SIZE];

      if (pthread_create(&_mthread1, NULL, (void *)thread_func1, NULL) != 0)
      {
           fprintf(stderr, "thread create failed\n");
           return -1;
      }

      epoll_fd = epoll_create(20);
      nfds = epoll_wait(epoll_fd, events, 5, -1);
        for (n = 0; n < nfds; ++n)
        {
            if (events[n].data.fd == listenfd)
                 printf("end11   time \n");   
           else if(events[n].events & EPOLLIN)
                 printf("end22   time \n");  
       }
      return 0;
}


epoll_ctl函数放线程里,程序阻塞在epoll_wait处,请问哪里有问题?如果把epoll_ctl函数放在主线程,就没问题,会打印"end11   time "。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-29 21:34 , Processed in 0.025363 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表