On Fri, Jul 31, 2009 at 8:44 PM, Bas Verhoeven <libev...@bserved.nl> wrote:

> Bas Verhoeven wrote:
> > Hello again,
> >
> > I'm currently working with libevent 2.02-alpha and I encountered a weird
> > problem when I try to exit the base loop from another thread.
> >
> >
> I seem to have found a workaround for this problem now.
>
> Instead of using 'event_base_dispatch(base)' I am now using
> 'event_base_loop(base, EVLOOP_NONBLOCK)' in a while(!shutdown) loop.
> This seems to work fine: the thread shuts down instantly and libevent
> cleans up.
>
> I also tried experimenting with the 'EVLOOP_ONCE' flag but then I
> encountered the same problem as when using no flags at all: libevent
> never really quits the loop, and I don't really get why it does that.
>
> I seem to be able to continue with this workaround, but I doubt that
> it's the best way to handle this.  So, if anyone has any suggestions on
> how to handle this differently, then please let me know.
>
>
>
That sounds like a pretty good work around, but have you though about
designing your system so that you dont have to break out of the loop in
order to exit?

When I handle a SIGINT, I close my listeners (and removing their events),
and tell my existing connections to finish what they are doing.  They can
then continue until they all close.  When the last client closes (or timeout
reached, and forcibly closed) and the event removed, then the loop
automatically exits, and my app shuts down.

As long as I have the listener events in the system, the loop keeps running,
but without them, it will not add any new clients, and the existing clients
will eventually close or be forcibly closed after a timeout (yes, it helps
to setup a timeout on your read/write handlers).  That way, the client nodes
can close cleanly.



-- 
"Be excellent to each other"
_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to