[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [Libevent-users] Bufferevent threadsafe opt questions.
- To: libevent-users@xxxxxxxxxxxxx
- Subject: Re: [Libevent-users] Bufferevent threadsafe opt questions.
- From: Nick Mathewson <nickm@xxxxxxxxxxxxx>
- Date: Mon, 15 Feb 2010 14:42:59 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Mon, 15 Feb 2010 14:43:06 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to :content-type:content-transfer-encoding; bh=U3gmbNCl6UjnO+pe4ERn77bBARlkP59TofkZJEHHBbE=; b=w/FyBL9MJd4Sy3E6/LewmNJ8VuRbg94jtu66V8GKf2O2V9vvzn8Bp6XJ3GBoNTCSli rmOsQ1lbEarc222LPxa1QyKRzg2UwWdBmPg0qWUQ0xeDMw6zf5issNR7zRy1pEoStbZ1 pF2fHu8mQvO/+1a3PXSy/UJATce9qpSXmQY7s=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; b=t1z9GB13yrBZezKgduLqpar9G65I22BUkDBqjX4lWA+MfAQbSiL4J+Rvx+b26KFHsE yGABofSDDmEQUC6EBqLxbAkAZ4T7y5Bp6aqUCP8O0ba/N/FhBB2MUJv51yEzNkkiZ55n WRqzL9fWhkOeQxjzL+7yE3XXBlc8RsEdrmBAM=
- In-reply-to: <915370.84873.qm@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- References: <915370.84873.qm@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
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.