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

Re: [Libevent-users] can I call event_del() on an event and then use event_add to add it again?



On Tue, Nov 10, 2015 at 09:00:03PM +0100, liu wen wrote:
> I register an timeout event as below:
> 
> struct event * fin_ev = (struct event *)malloc(sizeof(struct event));

Use event_new() instead of malloc()+event_set()

> event_set(fin_ev, -1, 0, on_fin_timeout, NULL);

Use event_assign() instead of event_set(), since assign allow to set
base too, and set must be deprecated, and please avoid API functions
that not allows to pass base (i.e. the one that use global base
instead, like event_set()).

> event_base_set(base, fin_ev);
> event_add(fin_ev, &tv) ;
> 
> and when a condition is satisfied when the timeout event is not triggered,
> I want to remove the event and add it again
> 
> can I do something like:
> 
>        if(condition){
>            event_del(fin_ev);
>            event_add(fin_ev, &tv)
>       }

Yep.

> 
> or
> 
>        if(condition){
>           event_set(fin_ev, -1, 0, on_fin_timeout, NULL);
>           event_base_set(base, fin_ev);

You can't call event_set() (i.e. event_assign()) for pending events, and
once you event_add() your event became pending, so if you will firstly
delete it them this is ok, i.e.:

    event_del(ev);
    event_assign(ev, ...);
    event_add(ev, ...);

>            event_del(fin_ev);
>            event_add(fin_ev, &tv)
>       }

Also you can take a look at http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.