Invoking event_add repeatedly on the same file descriptor with the same timeout

661 Views Asked by At

If I do the following

  1. invoke event_add for a particular file descriptor fd1 and timeout t1 = 5 seconds
  2. after 1 second, without waiting for any event to occur, I call event_add again for f1 and t1 = 5 seconds.

Will the second invocation of event_add cancel the earlier call? i.e. if the event does not happen, will the timeout now happen at 6 seconds instead of 5 seconds? Or, will we have 2 timeouts - one at 5 and another at 6 seconds

Does this behaviour differ between libevent versions 1 and 2? I did not find any documentation pertaining to this. I’m not able to comprehend this is from a reading of the code. I can obviously write a program to test this out but since the program has to work on different platforms, it would be better if there is a concrete documentation that describes the behaviour under these conditions.

1

There are 1 best solutions below

0
On

Quoting the official documentation (Making events pending and non-pending section):

If you call event_add() on an event that is already pending, it will leave it pending, and reschedule it with the provided timeout.

So you could expect the event being triggered for timeout after 6 seconds. Of course, any event occuring on the event structure (if associated with a file descriptor for example) between the two calls to event_add() will also reset the timer.


However, extending the answer with a non-asked piece of advice, pay attention to the following part:

If the event is already pending, and you re-add it with the timeout NULL, event_add() will have no effect.

Thus if you want to delete the timeout on an existing event, you could choose between:

  1. Using event_add() directly with a non-NULL timeout set to {0, 0}
  2. Deleting/Adding the event with event_del()/event_add(), having set a NULL timeout this time

I would recommend the latter.