eventxx  1.0.1
test-time.cpp

This is a timer usage example ported from libevent.

/*
* Compile with:
* c++ -I/usr/local/include -o time-test time-test.cpp -L/usr/local/lib -levent
*
* Wed 2006-12-27 - Modified by Leandro Lucarella <llucax+eventxx@gmail.com>
*
* Adapted to test the C++ inteface.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <eventxx>
int called = 0;
#define NEVENT 20000
eventxx::ctimer* ev[NEVENT];
void
time_cb(int fd, short event, void *arg)
{
called++;
if (called < 10*NEVENT) {
for (int i = 0; i < 10; i++) {
eventxx::time tv(0, random() % 50000L);
int j = random() % NEVENT;
if (tv.usec() % 2)
d.add(*ev[j], tv);
else
d.del(*ev[j]);
}
}
}
int
main (int argc, char **argv)
{
for (int i = 0; i < NEVENT; i++) {
/* Initalize one event */
ev[i] = new eventxx::ctimer(time_cb, NULL);
d.add(*ev[i], eventxx::time(0, random() % 50000L));
}
d.dispatch();
return (called < NEVENT);
}
eventxx::timer< ccallback_type > ctimer
Shortcut to C-style timer.
Definition: eventxx:491
Event dispatcher.
Definition: eventxx:549
void del(basic_event &e)
Removes an event.
Definition: eventxx:738
int dispatch(int flags=0)
Main dispatcher loop.
Definition: eventxx:764
void add(basic_event &e, int priority=DEFAULT_PRIORITY)
Adds an event to the dispatcher.
Definition: eventxx:588
Time used for timeout values.
Definition: eventxx:111
long usec() const
Gets the number of microseconds.
Definition: eventxx:134
This is the specialization of eventxx::timer for C-style callbacks.
Definition: eventxx:395