Passing data between event loops in multithreaded apps

[Libevent-users] Re: Passing data between event loops in multithreaded apps

Andrew Danforth adanforth at gmail.com
Thu Oct 5 21:42:35 EDT 2006


All,

Following up to my own message, I've finalized this code and put it
through some unit testing and I'd be interested in any feedback fellow
list members may have.

I've placed the source on my website @ http://www.weirdness.net/code/libevent

Thus far I don't have any documentation however I intend to add some
doxygen-based documentation in the future. I'll be particularly
motivated to write documentation if someone expresses interest in
reading it ;).

Thanks,

Andrew

On 9/11/06, Andrew Danforth <adanforth at gmail.com > wrote:
> Hey all, I just joined the list after noticing that there was
> discussion regarding libevent usage within threaded applications.
>
> I've implemented my own solution built on top of libevent that allows
> me to pass messages to a thread-specific event loop. I have a model
> similar to what William Ahern described where I have a listener thread
> that accepts inbound connections and then feeds them off to my
> multiple "worker" threads. The listener thread has to do a little
> work to determine which worker should receive the new connection but
> the overhead is negligible.
>
> Each worker thread is running its own event loop with
> event_base_dispatch. I create a socketpair, a mutex, and a message
> queue (of void pointers) for each worker thread. To send a message to
> a specific thread I lock the mutex, add the message to the queue, and
> write a byte down the right end of the socketpair if the queue was
> empty. Each worker has a persistent read event registered on its side
> of the socketpair and calls a callback that reads the byte, locks the
> queue, pops all the pending messages off the queue and processes them,
> etc.
>
> Using this solution I get the benefit of balancing clients across
> multiple threads and can still use libevent within that thread for
> polling and timeouts. I've been very pleased with the overall
> performance so far.
>
> I've been meaning to turn my existing code in to a library so I can
> more easily use it in future applications. I'd be happy to share what
> I come up with if anyone is interested in seeing it. I'm not sure if
> it would make sense to roll it in to the base libevent code since it
> has POSIX dependencies but perhaps Neils could make that call if the
> time comes.
>
> Andrew
>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值