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

Re: [Libevent-users] the relation between event loop and struct event variable lifetime and scope



On Wed, May 22, 2013 at 4:41 AM, wen lui <esolvepolito@xxxxxxxxx> wrote:
From stackoverflow I get to know that  if I define a struct event variable in a function which is different from where event_base_dispatch()
is defined, this event will not be added to the event loop, as below, ev1 will not be added to event loop.

   void func(){
     struct event ev1;

This event is on the stuck...
 

     event_set(&ev1, ...);
     event_add(&ev1, ...)

And you added it to the default event base.

If you dispatch the default base, you probably crash (soon if you are lucky). The memory used for the event will be overwritten by random data when another function is called.
 

   }

   int main(){
      func();

Here you dispatch on another base.
 

      event_base_dispatch(base);
 }

my problem is that I can't estimate the number of events in advance(think about discrete-event-driven simulation
if you know it, but I'm not using libevent
for this purpose). so I have to create one event each time a call back function or its sub-function wants to insert an event into the event_loop.
That's being said I need to create
many events that are local varibles which are not seen by
event_base_dispatch(base);

are there any workaround for my problem?
No, but reading libevent book may help.
http://www.wangafu.net/~nickm/libevent-book/

Basically what you want to do is:

- Create new events when needed with event_new()
- Add the new events to base with event_add()
- Free your events when done with event_free()