[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] New libevent user with some questions



On Wed, Oct 07, 2009 at 02:20:25PM -0500, Michael Kohout wrote:
> Hello all-
> I'm a new user using libevent 2.0.2a.  While the documentation on
> wangafu<http://www.wangafu.net/~nickm/libevent-book/> has
> helped me get started, I've got a couple basic questions then a couple of
> advanced questions.
> 
> 1)  Does the event loop free completed, nonpersisted events?

No; they are deleted (as if by event_del), but not freed (as if by
event_free).


> 2)  I'm developing a event-driven socket server that will eventually use
> bufferevents that also incorporates async database calls, for a multicore
> server.  Does it make sense to have multiple threads, each with their own
> event base?  If so, do any of you have suggestions on ways to structure
> stuff for optimal performance?  Also, is there any way to tell how many
> events are currently attached to an event base so I could balance the number
> of events being watched by each thread?

Personally, I'd suggest doing one event base, and setting up a thread
pool to run the work in other threads.  (Libevent will eventually have
better support for automatically running callbacks in separate worker
threads, and maybe even for multithreaded backends to automatically
and transparently run different dispatch functions, but that's
probably not in the works for 2.0 unless somebody turns up and writes
it.)


Other people who have more experience than I do with working with
multiple event bases.  The tricky issue here would probably be
load-balancing between them.

> 3)  What is the minimum I need to do to set up libevent so it's
> threadsafe?

Before you call any function that creates an event_base, initialize
the threading functions.  If you're using pthreads, just call
evthread_use_pthreads().  [You will need to link the libevent-pthreads
library as well as libevent.]

Note that Libevent 2.0 is still alpha code, so there are probably
still bugs.  In particular, the bug at
https://sourceforge.net/tracker/?func=detail&aid=2871656&group_id=50884&atid=461322
will cause a race condition in some multithreaded code.  I've attached
an almost-correct patch to that bug report; it should fix the problem
for most cases.  We should have a real fix in the next 2.0.x-alpha
release, with any luck.

>  Also, are the read/write buffers for bufferevents threadsafe? 

Yes, if you construct the bufferevent using the BEV_OPT_THREADSAFE
option.

> An example
> would be really helpful....I'm not much of a C coder (I'm actually
> developing this in common lisp using FFI) so I'm not totally conversant in
> raw pthreads and stuff.
>
> 4)  Can I pass the read/write buffers around to different events as the arg
> argument, or even to different threads?  The gist that I got from the docs
> is that they could be used for nonblocking IO.

I'm not sure what exactly you mean, so I'll answer more generally.  It
should be safe to call evbuffer functions from any thread.  Watch out:
the buffers will get freed when the bufferevent object is freed, so
don't keep pointers to them around if those pointers might last longer
than the bufferevent object.

> 
> 5)  Can I flush the output buffer automatically when the underlying socket
> is asked to close?

There's no built-in way to do that automatically, but you could probably
do it by having an event callback for the bufferevent that noticed
the EOF event and called bufferevent_flush() in response.

hope this helps,
-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.