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

Re: [Libevent-users] Timeout Event Not Working?



Thanks Jonas. I fixed the code like you suggested, using event_base_loop(global_event_base, 0) and evtimer_add(dummy_event, &five_seconds). Now the dummy_callback() is indeed called, but the event_base_loop() exited with a failed status, as show below:

***entering main()
***entering EventLoop()
***dummy_event=0x60e690
***in dummy_callback()
event_base_loop() failed
***exiting EventLoop()
***exiting main()

Any idea what might be wrong? Following is the current code:

#include <stdlib.h>
#include <stdio.h>
#include "event2/event.h"

struct event_base *global_event_base;

static void dummy_callback(evutil_socket_t fd, short what, void *arg)
{
  printf("***in dummy_callback()\n");
}

static void* EventLoop(void *arg)
{
  printf("***entering EventLoop()\n");
  global_event_base = event_base_new();
  if (!global_event_base) {
    printf("event_base_new() failed\n");
    exit (-1);
  }

  struct timeval five_seconds = {5,0};
  struct event *dummy_event =
    evtimer_new(global_event_base, dummy_callback, arg);

  printf("***dummy_event=%p\n", dummy_event);

  evtimer_add(dummy_event, &five_seconds);

  //  int ret = event_base_loop(global_event_base, EVLOOP_NONBLOCK);
  int ret = event_base_loop(global_event_base, 0);
  if (ret!=0)
    printf("event_base_loop() failed\n");


  printf("***exiting EventLoop()\n");
  return NULL;
}

int main()
{
  printf("***entering main()\n");
  EventLoop(NULL);
  printf("***exiting main()\n");
  fflush(stdout);
  return 0;
}



On Fri, Jan 29, 2010 at 2:45 AM, Jonas Romfelt <jonas@xxxxxxxxxx> wrote:
Hi,

if I'm not mistaken your call to event_base_loop() will not wait for
the timer event you added to expire beacuse the EVLOOP_NONBLOCK flag
is set. Simply because your 5 seconds timer has not yet expired when
the event_base_loop() is called.

Try: event_base_loop(global_event_base, 0) or
event_base_loop(global_event_base, EVLOOP_ONCE) if you want to exit
the loop after the timer expired.

Also, you could change event_add(dummy_event, &five_seconds) to
evtimer_add(dummy_event, &five_seconds). Event though it's just a
macro you never know when something under the API may change...

cheers,
Jonas Romfelt



2010/1/29 Donghua Xu <donghua@xxxxxxxxx>
>
> Hi, I just wrote a simple program trying to use the timeout event in libevent 2.0.3-alpha. The source code is at the end of the email. I'm expecting to see a print out of "***in dummy_callback()", but this line does not show up at all. What I am seeing is only:
>
> ***entering main()
> ***entering EventLoop()
> ***dummy_event=0x60e690
> ***exiting EventLoop()
> ***exiting main()
>
> What am I doing wrong in the following source code? A lot of thanks in advance...
>
>
>
> #include <stdlib.h>
> #include <stdio.h>
> #include "event2/event.h"
>
> struct event_base *global_event_base;
>
> static void dummy_callback(evutil_socket_t fd, short what, void *arg)
> {
>   printf("***in dummy_callback()\n");
> }
>
> static void* EventLoop(void *arg)
> {
>   printf("***entering EventLoop()\n");
>   global_event_base = event_base_new();
>   if (!global_event_base) {
>     printf("event_base_new() failed\n");
>     exit (-1);
>   }
>
>   struct timeval five_seconds = {5,0};
>   struct event *dummy_event =
>     evtimer_new(global_event_base, dummy_callback, arg);
>
>   printf("***dummy_event=%p\n", dummy_event);
>
>   event_add(dummy_event, &five_seconds);
>
>   int ret = event_base_loop(global_event_base, EVLOOP_NONBLOCK);
>   if (ret!=0)
>     printf("event_base_loop() failed\n");
>
>
>   printf("***exiting EventLoop()\n");
>   return NULL;
> }
>
> int main()
> {
>   printf("***entering main()\n");
>   EventLoop(NULL);
>   printf("***exiting main()\n");
>   fflush(stdout);
>   return 0;
> }
>
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.