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

Re: [Libevent-users] Bufferevent threadsafe opt questions.



On Tue, Feb 9, 2010 at 3:39 AM, Jj Jack <jjjack94@xxxxxxxxx> wrote:
> Hello again,
>
> I have some questions related to BEV_OPT_THREADSAFE, specifically.. when exactly is the option's use required?

If a bufferevent is used from two different threads, or if it is used
from any thread besides the one running its event_base's loop, it
needs to be locked.

> If I have an application that has two threads, one that writes to the output buffer only, and another that reads from the input buffer only, with at max one thread at a time reading from those two buffers (i.e, 2 threads maximum at a time, but always on opposite buffers), do I still need locks?

Yes, for two reasons, one trivial and one signficant:

 Significant reason: there's a third operation you aren't considering
here: the event_base is activating the events that implement your
bufferevent in the thread where you're running event_base_loop().  It
can only possibly be running in one of the two threads you mention (or
in a third thread entirely).  It will both add data to the read
buffer, and drain data from the write buffer, so it will necessarily
race with one (if not both) of the operations you mentioned.

 Trivial reason: We didn't design it to support lockless multithreaded
operation, so even if it somehow worked, it would only be working by
accident. :)

> What if I was to read some data, disable the bufferevent, and then re-enable it later from another thread that would add some data to it (or something), (i.e, maximum one thread at a time here, but multiple threads over time), is that safe?

Again, the event_base will be running the event callbacks that
implement the bufferevent from whatever thread its loop is running in.
 So it would be safe if you access the bufferevent from only one
thread at a time, so long as the bufferevent's event_base is only
running in that thread too.

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