about a multi-ev_loop server

debuguo wateraworld at gmail.com
Mon Jul 16 02:48:01 CEST 2012

Hello everyone,
I am working at a multi-ev_loop network program based on libev. The server
has several ev_loops, one is for accept the connect from the client, the
others for recv and send data with the client.
For example, I create 4 ev_loop, one ev_loop for accept, the other 3
ev_loop for receive and send(read/write). every ev_loop has a ev_async to
In accept ev_loop I put a pair of ev_io and ev_timer to monitor the file
descriptor returned from socket, once the ev_io get EV_READ event, the
server accept the connect and get the file descriptor, then dispatch it to
one read/write ev_loop. the point is how to do the dispatching.
This is my way according to some questions and answers in the mail list. I
put another ev_async in the accept ev_loop except for ev_io/ev_timer, and
ev_async_send to other read/write ev_loop when EV_READ event occurs. as I
mentioned before in "*a demo code considering if ev_async event missing*",
Not every ev_async_send can invoke the ev_loop to one callback when
ev_async_send called very frequently. so I used a pthread_cond_t to
pthread_cond_wait() the last ev_async_send() invoke into the callback, and
pthread_cond_signal() the pthread_cond_t in the async callback. I wonder if
this is a good policy to dispatch the file descriptor, and if there is a
better one.
In read/write ev_loop, I put a pair of ev_io and ev_timer to monitor the
file descriptor dispatched from accept ev_loop. once the ev_io get EV_READ
event, read data from the fd; get EV_WRITE event, write data to the fd.
I used ev_async to exit ev_loops, when exit, in the async callback, just
ev_TYPE_stop any watchers, then ev_break.
It seems running well. Is there any improper thing in my description?
I just wonder if there is any better ways to dispatch/notify from one
ev_loop to the other, and if there is any better solutions to exit ev_loop.
Thank you all, especially Marc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20120716/cfe97b17/attachment.html>

More information about the libev mailing list