带有第二个参数= 0的eventfd_write()不起作用?

时间:2015-10-06 06:05:56

标签: c++ epoll

当使用第二个参数= 0调用eventfd_write()时,epoll_wait()永远不会返回,但是当参数设置为1. epoll_wait()返回。

以下是我重现的方式: ./bug 0

永远不会回来。

./ bug 1

它返回。

以下是代码:

If LCase(Trim(c)) = "to be uploaded" Or LCase(Trim(c)) = "to be loaded" Then ...

以下是我的编译方式:

#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;
}

这是我的glibc版本:

g++ -Wall bug.cpp -o bug -O3 -lpthread

这是我的g ++版本:

$ 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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

简短回答: 阅读documentation

更长的回答: &#34; write(2)调用将其缓冲区中提供的8字节整数值添加到计数器&#34;

&#34;如果计数器的值大于0,则文件描述符是可读的(select(2)readfds参数; poll(2)POLLIN标志)。&#34;

因此,写0不会向计数器添加任何内容,值0表示正在等待的文件描述符未就绪。任何非零值都应该有效(除了保留的0xffffffff)。