0

Most applications that I have made with libevent involve one read callback and whenever a new connection is opened for a request I allocate a new event with ::event_new() and then add that event to the event base via ::event_add()

But the problem with this is that every time a new request is opened I allocate memory and add that to the event loop, when in reality I should be sharing callbacks and events for file descriptors. Granted memory allocation is not a bottleneck for such an application, does libevent offer some way to share events with multiple file descriptors?

Curious
  • 20,870
  • 8
  • 61
  • 146
  • 1
    What does `sharing callbacks and events for file descriptors` mean in your question? Memory allocations are unavoidable to store the state of a connection for processing. An `event` has its state that is completely different from any other event. How can you share that to achieve different functionalities? And, `file descriptors` when not used (closed) are reused by the OS for `select`, `poll`, `epoll`, etc. Can you elaborate a bit more what do you exactly want to do? – Azeem Jun 16 '17 at 04:42
  • @Azeem i asked since all the read events i have are the same except for the file descriptor they wait on – Curious Jun 16 '17 at 04:59
  • Can you give a code example? Kindly edit your question with some supporting code example. – Azeem Jun 16 '17 at 05:01

1 Answers1

1

You should not share events between file descriptors. That would create chaos in your app. event_new() creates an event that is attached to your file descriptor.

You could try reassigning spent events using event_assign() but the libevent documentation specifically states that this is not recommended. Plus, managing spent events in a pool of some sort may not be as trivial as it seems, and there probably would not be any noticeable performance gains.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Could you give an example of what you mean by chaos? All the read events i create are the same except for the given file descriptor. Im just trying to understand the correct usage snd patterns here – Curious Jun 16 '17 at 04:58
  • Events are tied to the socket, and fire on events on that socket. Attaching an event to more than one active socket will lead to undefined behaviour. This means that your program will either bomb straight away, or act in an undefined manner - which means that any kind of bug can happen from that point on, like events being fired on the wrong socket, a dead socket, a memory leak, untraceable memory corruption and the likes. – Michaël Roy Jun 16 '17 at 05:14