简体   繁体   English

第2个参数= 0的eventfd_write()不起作用?

[英]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.

相关问题 概念强加第二个参数是 int 类型 - Concepts impose the 2nd argument is of type int 收集的第二个模板; reverse_iterator无法编译 - Template for 2nd last in collection; reverse_iterator doesn't compile 为什么第一次正确放置新的创建/添加对象,但是第二次没有正确放置? - Why does placement new create/add object correctly 1st time but doesn't the 2nd time? 如何在C ++ Sprintf 2nd(格式)参数中传递变量? - How to pass variable in C++ Sprintf 2nd(format) argument? boost :: bind不适用于指针参数 - boost::bind doesn't work with pointer argument 为什么C ++程序在打印字符之前不像第二次迭代那样等待第二次迭代的时间 - Why doesn't C++ program wait a sec for 2nd iteration like it does for 1st iteration before printing a character C ++-具有默认参数的模板模板参数不起作用 - C++ - Template template parameter with default argument doesn't work 重复使用可变参数函数参数不起作用 - Repeated use of a variadic function argument doesn't work 为什么模板参数推导在 C++ 中不起作用? - Why template argument deduction doesn't work in C++? 类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数? - Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM