3

私はepollに基づいた単純なサーバークラスを書いています。目を覚ますためにepoll_wait()、eventfdを使うことにしました。シンプルなイベントコミュニケーションに向いているとのことで、そう思います。だから私は自分のイベントを作成し、それに時計を置きました:

_epollfd = epoll_create1(0);
if (_epollfd == -1) throw ServerError("epoll_create");
_eventfd = eventfd(0, EFD_NONBLOCK);
epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = _events;
if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &evnt) == -1)
    throw ServerError("epoll_ctl(add)");

メッセージ待機ループの後半、別のスレッドで:

    int count = epoll_wait(_epollfd, evnts, EVENTS, -1);
    if (count == -1)
    {
        if (errno != EINTR)
        {
            perror("epoll_wait");
            return;
        }
    }

    for (int i = 0; i < count; ++i)
    {
        epoll_event & e = evnts[i];
        if (e.data.fd == _serverSock)
            connectionAccepted();
        else if (e.data.fd == _eventfd)
        {
            eventfd_t val;
            eventfd_read(_eventfd, &val);
            return;
        }
    }

そしてもちろん、サーバーを停止するコードは次のとおりです。

eventfd_write(_eventfd, 1);

epoll_wait()説明できない理由で、イベントに書いただけでは目を覚ますことができませんでした。最終的に、これはいくつかのデバッグセッションで機能しました。

これが私の回避策ですEPOLLOUT。fdが書き込み可能になるたびにイベントがトリガーされることを知っているので、停止コードを次のように変更しました。

epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = EPOLLOUT;
if (epoll_ctl(_epollfd, EPOLL_CTL_MOD, _eventfd, &evnt) == -1)
    throw ServerError("epoll_ctl(mod)");

今は動作しますが、このようにすべきではありません。

これは難しいことではないと思います。私は何を間違えましたか?

ありがとう

4

2 に答える 2

3

私のために働きます。参考までに、完全なCコードを次に示します。「eventfd_write」、「1」、および「DING:1」を出力します。Linux 2.6.35-30-generic#56-UbuntuSMPでテスト済み。

#include <stdio.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <pthread.h>
#include <stdlib.h>

int _epollfd, _eventfd;

int init()
{
    _epollfd = epoll_create1(0);
    if (_epollfd == -1) abort();
    _eventfd = eventfd(0, EFD_NONBLOCK);
    struct epoll_event evnt = {0};
    evnt.data.fd = _eventfd;
    evnt.events = EPOLLIN | EPOLLET;
    if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &evnt) == -1)
        abort();
}

void *subprocess(void *arg)
{
    static const int EVENTS = 20;
    struct epoll_event evnts[EVENTS];
    while (1) {
        int count = epoll_wait(_epollfd, evnts, EVENTS, -1);
        printf("%d\n", count);
        if (count == -1)
        {
            if (errno != EINTR)
            {
                perror("epoll_wait");
                return NULL;
            }
        }

        int i;
        for (i = 0; i < count; ++i)
        {
            struct epoll_event *e = evnts + i;
            if (e->data.fd == _eventfd)
            {
                eventfd_t val;
                eventfd_read(_eventfd, &val);
                printf("DING: %lld\n", (long long)val);
                return NULL;
            }
        }
    }
}

int main()
{
    pthread_t th;
    init();
    if (pthread_create(&th, NULL, subprocess, NULL) != 0)
        abort();
    sleep(2);
    printf("eventfd_write\n");
    eventfd_write(_eventfd, 1);
    sleep(2);
}
于 2012-09-02T13:20:10.870 に答える
1

複数のスレッドを使用している場合はeventfd_write、各スレッドの最後で呼び出しをチェーンする必要があります。これは1つのオプションにすぎません。

于 2012-09-03T22:53:33.440 に答える