2013-03-21 90 views
0

最近,我有一個使用libevent的小套接字服務器程序。libevent無法讀取一個打開的套接字描述符

總之,它做下列工作。

void read_function(int fd, short event, void* arg) { 
    printf("callback is called!\n"); 

    // read from fd, and send a reply to fd! 
} 

void accept_thread_function() { 
    int fd = accept(...); 
    struct event* ev_read = new struct event(); 
    memset(ev_read, 0, sizeof(struct event)); 
    event_set(ev_read, fd, EV_READ|EV_PERSIST,read_function,ev_read); 
    event_add(ev_read, 0); 
} 

int main() { 
    event_init(); 
    THREAD a = start 'accept_thread_function' as a thread; 
    event_dispatch(); 
    THREAD::join(a); 
} 

問題是,read_function從來沒有被調用過。

傳入連接被正確接受。 (嘆氣)

我在等待你對這個問題的評價。

預先感謝您。

回答

0

首先來看看(免費)精彩的書上libevent的尼克馬修森寫:

http://www.wangafu.net/~nickm/libevent-book/Ref1_libsetup.html#_locks_and_threading

然後,你需要調用之前,爲了確保該事件被添加event_dispatch()。

+0

感謝雷米。這是一個很棒的評論。 – 2013-03-29 06:19:02

+0

無論如何,我已經轉而使用libev而不是libevent。 libev給了我很多可靠的結果,而且它很容易使用。 – 2013-03-29 06:19:40