[英]eventfd_write() with 2nd argument=0 doesn't work?
when eventfd_write() is called with 2nd argument=0, epoll_wait() never returns, but when the argument is set to 1. epoll_wait() returns. 当使用第二个参数= 0调用eventfd_write()时,epoll_wait()永不返回,但是当参数设置为1时,epoll_wait()返回。
Here is how I reproduce: ./bug 0 这是我的再现方式:./bug 0
It never returns. 它永远不会回来。
./bug 1 ./错误1
It returns. 它返回。
Here is the code: 这是代码:
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <assert.h>
#include <cstdlib>
#include <iostream>
int value = 0;
int efd = 0;
void* start(void* p) {
std::cout << __PRETTY_FUNCTION__ << ": going to sleep for 5 sec" << std::endl;
sleep(5);
std::cout << __PRETTY_FUNCTION__ << ": going to call eventfd_write() with value=" << value << std::endl;
const int rc = eventfd_write(efd, value);
assert(0 == rc);
return NULL;
}
int main(int argc, char** argv) {
const int epFD = epoll_create1(0);
assert(-1 != epFD);
efd = eventfd(0, 0);
assert(-1 != efd);
struct epoll_event event;
event.data.fd = efd;
event.events = EPOLLIN;
epoll_ctl(epFD, EPOLL_CTL_ADD, efd, &event);
value = strtoul(argv[1], NULL, 10);
const uint32_t nEvents = 2;
struct epoll_event events[nEvents];
pthread_t threadID;
const int rc = pthread_create(&threadID, NULL, &start, NULL);
assert(0 == rc);
sleep(1);
std::cout << __PRETTY_FUNCTION__ << ": going to wait for event" << std::endl;
int n = epoll_wait(epFD, events, nEvents, -1);
assert(n > 0);
std::cout << "okay" << std::endl;
return 0;
}
Here is how I compiled: 这是我的编译方式:
g++ -Wall bug.cpp -o bug -O3 -lpthread
Here is my glibc version: 这是我的glibc版本:
$ rpm -qa | grep glibc
glibc-common-2.12-1.107.el6.x86_64
glibc-static-2.12-1.107.el6.x86_64
glibc-headers-2.12-1.107.el6.x86_64
glibc-2.12-1.107.el6.x86_64
glibc-devel-2.12-1.107.el6.x86_64
Here is my g++ version: 这是我的g ++版本:
$ g++ --version
g++ (GCC) 4.6.2 20111027 (Red Hat 4.6.2-1)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Any idea? 任何想法?
Short answer: Read the documentation 简短答案:阅读文档
Longer answer: "A write(2) call adds the 8-byte integer value supplied in its buffer to the counter" 更长的答案:“ write(2)调用将其缓冲区中提供的8字节整数值添加到计数器中”
"The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the counter has a value greater than 0." “如果计数器的值大于0,则文件描述符是可读的(select(2)readfds参数; poll(2)POLLIN标志)。”
So, writing 0 doesn't add anything to the counter, and a value of 0 means that the file descriptor being waited on is not ready. 因此,写入0不会为计数器添加任何内容,而值为0表示等待中的文件描述符尚未准备好。 Any non-zero value should work (except 0xffffffff which is reserved).
任何非零值都应该起作用(保留的0xffffffff除外)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.