您的位置:首页 > 移动开发

Passing data between event loops in multithreaded apps

2009-05-25 12:48 274 查看

[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

Previous message: [Libevent-users] Can't get event-test.c to work on Mac OS X 10.4.8

Next message: [Libevent-users] Can't get libevent ( event-test.c ) to work on OS
X ( 10.4.8 )!

Messages sorted by:

[ date ]
[ thread ]
[ subject ]
[ author ]

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

>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: