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

Re: [Libevent-users] Multi-threaded support in libevent



Hi Steffen,

Thank you for your useful advice. Actually, as  you said, if I let libevent handle the networking stuff within one thread, I wonder that is there a bottle neck that happens when client sends a lot of messages to four sockets?

I use multiple threads on different ports for the same service. Here is my implementaton,

Client A sends command X to thread 1 on socket 1234, then thread 1 will receive command X and execute the command.
Client B sends command Y to both thread 1 and 2 on socket 1234 and 1235, respectively. Two threads receive the command, but thread 2 send a signal to thread 1, and wait. Thread 1 will execute the message, and after it finishs, it send a signal to thread 2 to update the result of command Y.

Because I use the libevent and threads together, so I am very confused to design my implementation. In this case, should I use one event_base for all theads, or each event_base for each thread?

@Jan: Thank you. I will take a look for your suggestion.

Best Regards,
Loan 





On Thu, Feb 23, 2017 at 9:12 PM, Steffen Christgau <mail@xxxxxxx> wrote:
Hi Loan,

although it might not answer your question, I'm wondering if your
application really needs threads for the different sockets. From what I
read, I conclude that each of the four threads handles the packet, i.e.
the messages/requests that are sent to the socket associated with that
particular thread.

However, do the threads do different work, i.e. do they provide
different services on the sockets. Or do you use multiple threads
because you are offering the same service on different ports (and, thus,
on different interfaces)? Nevertheless, it appears to me that the
threads are not created by to distribute the work of processing the
messages that are received on either socket.

Thus, you might not need different threads from my point of view. Just
create your four sockets and let libevent handle the networking stuff
within one thread.

Look at the event_new function. To do different things with the
different sockets (e.g. handle different application protocols) just
provide different callbacks. Supply an according arg parameter if you
want to as well.

http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html provides
with you with a short introduction and also shows you how to add
multiple file descriptors to the same event_base.

Hope this helps you.

Regards, Steffen

On 23.02.2017 10:31, Tôn Loan wrote:
>
> Hello list,
>
> I'm new to libevent and trying to learn some more about it for
> potential use in a project. Here is my problem.
>
> I have one server that has 5 threads. Each thread bounds to a different UDP
> port (separate socket). Each thread listens on separate socket and receive
> the message from client.
> I used each event_base for each thread and added signal events to each
> event_bases But only one event_base will receive signals.
>
> So, is there any solution for each event_base that will receive its own
> signals?
> Any ideas or sample code would be extremely helpful. Thank you so much.
>
> Best Regards,
> Loan Ton

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