[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] Timerout procedure can't enter twice
- To: libevent-users@xxxxxxxxxxxxx
- Subject: [Libevent-users] Timerout procedure can't enter twice
- From: Michael <michaelxmail@xxxxxxxxx>
- Date: Tue, 11 Sep 2012 15:54:51 -0600
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Tue, 11 Sep 2012 17:55:34 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=6ADnyZLNbVk1+9+E6E05AVVGuxLPQR3K725TdaSL0rQ=; b=0QHaDvApKFbedLuywLd706iU+BKvSzFQXrfe/u9PG5ycvKvDC0KtjOF8ene4J8gxYY PRI9IPxdc5joVTZQOu7G226a6btRZBcqLZ5q34rCT090Dn0Xdacot3a4qZjHRB4UfrxD /VrEBrYZXkNuxUaE1hY8lX5H0mqXFA6Cjz7NTUQ/6o7OWQyM+72C0nAYVq/tYZpWpj+k PUvDoTzFZeGeHORK4vjS9lKQM2JllXAlGb7/9dkZ9wau+drNa+o+/fYJFUkdkY+PNjkL As2cRVYgsCegyOz7hoeZ8RV1HjXvJ8nsqyvQYuwpm4EyM4q+K/7o4J0chMM1U2hUjUAh ojog==
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
Hi ,
I'm using libevent to create timer procedure.
The attached program will call the timeout function at the first time.
But it won't call the timeout function after I rewrite the timer.
Attached please find the test code.
Is there anything I'm missing ?
Thanks!
Michael
/*
* XXX This sample code was once meant to show how to use the basic Libevent
* interfaces, but it never worked on non-Unix platforms, and some of the
* interfaces have changed since it was first written. It should probably
* be removed or replaced with something better.
*
* Compile with:
* cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
*/
#include <sys/types.h>
#include <event2/event-config.h>
#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#endif
#include <time.h>
#include <unistd.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>
struct timeval lasttime, lasttime2, lasttime3, lasttime4, lasttime5;
int event_is_persistent;
static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
struct timeval newtime, difference;
struct event *timeout = arg;
double elapsed;
evutil_gettimeofday(&newtime, NULL);
evutil_timersub(&newtime, &lasttime, &difference);
elapsed = difference.tv_sec +
(difference.tv_usec / 1.0e6);
printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
(int)newtime.tv_sec, elapsed);
lasttime = newtime;
}
void main()
{
struct event *timeout;
struct timeval tv;
struct event_base *base;
int flags;
event_is_persistent = 0;
flags = 0;
/* Initalize the event library */
base = event_base_new();
/* Initalize one event */
timeout = event_new(base, -1, flags, timeout_cb, (void*) timeout);
evutil_timerclear(&tv);
tv.tv_sec=2;
tv.tv_usec=0;
event_add(timeout, &tv);
printf("Timer writed \n");
evutil_gettimeofday(&lasttime, NULL);
event_base_dispatch(base);
usleep(5000*1000);
printf("rewrite timer \n");
timeout = event_new(base, -1, flags, timeout_cb, (void*) timeout);
evutil_timerclear(&tv);
tv.tv_sec=2;
tv.tv_usec=0;
event_add(timeout, &tv);
usleep(5000*1000);
return (0);
}