Hi all,

I'm just getting started with libevent and I was wondering how I might implement a simple deadline timer with it.

I'd like to be able to use do something like:

        void do_something(...) ...
deadline_timer_wait(5, do_something); /* _non-blocking_, call do_something() in 5 seconds */
        ... perhaps less than 5 seconds later ...
deadline_timer_nevermind(); /* whatever we're waiting on, stop waiting, don't call do_something() */


However I'm a bit confused as to how event_dispatch() is supposed to work. Do I need to fork and then have the child call event_dispatch() in a loop:

        ...

        struct event_base* current_base;
        struct event ev;
        pid_t pid;

        current_base = event_init();
        if ((pid = fork()) == 0) {
                if (event_reinit(current_base) == -1) {
                        fprintf (stderr, "FAILED (reinit)");
                        exit(1);
                }
        
                while (true) {
                        event_dispatch();
                }
        }

        ...

        timeout_set(&ev, do_something, 0);

        ...

/* Then, whenever I want to start a deadline timer for calling do_something: */
        timeout_add(&ev, (struct timeval){ 5, 0 });

        ...

        /* Later, during clean up: */
        kill(pid, SIGTERM);

        ...

The problems I encountered with this approach is that the child process pegs the processor hard. And assuming that can be fixed, I'm not even sure this approach is what I need to get my deadline timer idea off the ground.

Any ideas?

--
Andrew Troschinetz
Applied Research Laboratories

_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to