4

I have the following code:

#include <stdio.h>
#include <sys/time.h>
#include <event.h>

void say_hello(int fd, short event, void *arg){
        printf("Hello\n");
}

int main(int argc, const char* argv[])
{
  struct event ev;
  struct timeval tv;

  tv.tv_sec = 3;
  tv.tv_usec = 0;

  event_init();
  evtimer_set(&ev,say_hello,NULL);
  evtimer_add(&ev, &tv);
  event_dispatch();

  return 0;
}

Problem is "hello" gets printed once and then the program exits...

I want it to output "hello" indefinitely.

How to do this? Many thanks in advance,

Eamorr
  • 9,872
  • 34
  • 125
  • 209

5 Answers5

7

Just to clarify Basile's solution:

I was confused as well until I realized that "timer" in this context refers to a single shot timer. What we need is an interval timer; which requires the EV_PERSIST flag in libevent.

struct timeval time;
time.tv_sec = 1;
time.tv_usec = 0;

event_set(&my_event, 0, EV_PERSIST, my_function, NULL);
evtimer_add(&my_event, &time);
seo
  • 1,959
  • 24
  • 18
6

change the code in main to read

evtimer_set(&ev,say_hello,&ev);

and make your say_hello function

void say_hello(int fd, short event, void *arg){
    struct event *ev = arg;
    struct timeval tv;


    printf("Hello\n");
    tv.tv_sec = 3;
    tv.tv_usec = 0;

    evtimer_add(ev, &tv);
}
nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    This is the only correct answer on this page that applies to the libevent library version (`1.4.x`) that the OP was asking about. – quant Sep 06 '15 at 06:49
2

Here is the example for libevent2:

#include <event2/event.h>

static int n_calls = 0;

void cb_func(evutil_socket_t fd, short what, void *arg)
{
    struct event *me = arg;

    printf("cb_func called %d times so far.\n", ++n_calls);

    if (n_calls > 100)
       event_del(me);
}

void run(struct event_base *base)
{
    struct timeval one_sec = { 1, 0 };
    struct event *ev;
    /* We're going to set up a repeating timer to get called called 100
       times. */
    ev = event_new(base, -1, EV_PERSIST, cb_func, event_self_cbarg());
    event_add(ev, &one_sec);
    event_base_dispatch(base);
}
Guy L
  • 2,824
  • 2
  • 27
  • 37
1

Did you read evtimer_set(3) man page? Please read it again.

You probably want to use the flag EV_PERSIST or call evtimer_add from your say_hello callback.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0
#include <stdio.h>
#include "ev.h"

static void timer_cback_caller(EV_P_ ev_timer* w, int revents)
{
    const int new_timeout_in_ms = 1200; // new delay between timer ticks

    printf("timer tick\n");

    w->repeat = new_timeout_in_ms / 1000.;
    ev_timer_again(EV_A_ w);
}


int main()
{
    struct ev_loop *loop = ev_default_loop(0);
    ev_timer timer;

    const int start_delay_in_ms = 1000; // delay before first timer tick
    const int timeout_in_ms = 1500; // delay between every timer tick

    ev_timer_init(&timer, timer_cback_caller, start_delay_in_ms/1000., timeout_in_ms/1000.);
    ev_timer_start(loop, &timer);
    ev_run(loop, 0); // will never return

    return 0;
}

More examples in libev documentation

vasily-vm
  • 101
  • 3