File: testtimer.cc

package info (click to toggle)
atom4 4.1-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 947
  • sloc: cpp: 4,461; makefile: 54; sh: 21; perl: 6
file content (63 lines) | stat: -rw-r--r-- 1,426 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Test for timer code
 */

#include <stdio.h>
#include "event.h"


class timerA : public timerhandler {
public:
  timerA() : timerhandler(STATIC) {}
  void tick(eventloop *src, timeval t) {
    printf("(A:%p) Tick (%ld,%ld)\n", this, t.tv_sec, t.tv_usec);
  }
};

class timerB : public timerhandler {
  int *flag;
public:
  timerB(int *exitflag) : timerhandler(STATIC), flag(exitflag) {}
  void tick(eventloop *src, timeval t) {
    printf("(B:%p) Tick (%ld,%ld), setting exit flag\n", this, t.tv_sec,
           t.tv_usec);
    (*flag)++;
  }
};

class canceller : public timerhandler {
  eventloop *loop;
  int id;
public:
  canceller(eventloop *eloop, int timer_id) : timerhandler(DYNAMIC),
	loop(eloop), id(timer_id) {}
  ~canceller() {
    printf("The reaper departs!\n");
  }
  void tick(eventloop *src, timeval t) {
    printf("Cancelling timer %d\n", id);
    loop->unschedule(id);
  }
};

int main() {
  eventloop loop;
  int quitflag=0;
  timerA x, y;
  timerB z(&quitflag);
  canceller *reaper;
  int xid, yid, zid;

  xid=loop.schedule(10,0, &x);		// tick once after 10 seconds
  yid=loop.schedule(1,0, 2,0, &y);	// tick after 1 second, and every 2
					// seconds thereafter
  zid=loop.schedule(30,0, &z);		// tick after 30 seconds and set
					// quit flag

  reaper = new canceller(&loop, yid);
  loop.schedule(25,0, reaper);		// kill timer y after 25 seconds

  // Begin the fun!
  loop.run(&quitflag);
}