大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-3-16
|
本帖最后由 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 "。
|
|