[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: RE: [Libevent-users] std::threads
> Thanks Tom. "event bases + threads (each thread running an event base)", that is what I have done. Each connection is a thread with a event_base + a socket_bufferevent. But when a connection has no communication during sometime, the associated thread is idle. That's the main problem I concern.
Hi Michael,
According to what you wrote before this is not 100% correct:
- you have *one event_base*, *multiple threads*
- while Tom suggesting *event_base per thread*
But this is not important though.
> For example, there are 10 connections with the server, and 10 threads on the server are responsible for the connections, e.g., thread-1 is associated with connection-1, with event_base-1 and socket_bufferevent-1. If the client with the connection-1 stop sending messages to the server, the thread-1 is stuck. Becuase thread-1 is running dispatch(event_base-1), which is a blocking API.
dispatch() is a loop that have events for monitoring (see
epoll_wait() and other multiplexing waiters).
> So, I want to put all of the socket_bufferevents in one event_base, and only one thread is runing the event_base. When one of the socket_bufferevents can read or write, the associated callback will pass the read/write data to a thread to process. I suppose this model is more reasonable, right?
So to summarize you want to move all events handling into separate
base=thread, and send data to it (how? using pipes?), if so the answer
is it depends from what data you have it is very reasonable if you
callbacks are simple (IOW fast) that you can use this, otherwise it will
be better to have a thread pool I think (but also it depends on many
things, since you can defer some work into other threads and use main
only for event loop).
> The question is how can I implement that with std::thread. Because our project use std::thread. I see libevent can use pthread (even though there is no example in the documentation) but I am don't know how to use it with std::thread. The explaination is so simple I do not know how to implement it with std::thread.
All you need to do is move something like this into std::thread:
event_base_loop(base, 0);
And you can use pipes/socketpair to communicate with clients
connections.
Anyway here are examples of what I'm talking about:
- https://github.com/azat/boostcache/blob/libevent-aio/src/kernel/net/commandserver.cpp (cpp)
- https://github.com/ellzey/libevhtp/blob/develop/evthr.c (c)
Cheers,
Azat.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.