summaryrefslogtreecommitdiff
path: root/ACE/examples/APG/Timers/Alarm.cpp
blob: 99e9d49094d495d6e54507b95ab95b4e05d5f345 (plain)
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
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_time.h"

// Listing 1 code/ch20
#include "ace/Timer_Queue_Adapters.h"
#include "ace/Timer_Heap.h"

typedef ACE_Async_Timer_Queue_Adapter<ACE_Timer_Heap> Timer;
// Listing 1

class CB : public ACE_Event_Handler
{
public:
  CB (int id) : id_(id) { }

  virtual int handle_timeout (const ACE_Time_Value &,
                              const void *arg)
  {
    ACE_TRACE ("CB::handle_timeout");

    const int *val = static_cast<const int*> (arg);
    ACE_ASSERT ((*val) == id_);

    ACE_UNUSED_ARG (val);

    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Timer expired\n")));
    return 0;
  }

private:
  int id_;
};

// Listing 2 code/ch20
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  // Create the timer such that it blocks all signals
  // when it goes off.
  Timer timer;

  // Schedule a timer to go off 2 seconds later and then
  // after every 4 seconds.
  CB cb (1);
  int arg = 1;
  ACE_Time_Value initial (2);
  ACE_Time_Value repeat (4);
  initial += ACE_OS::gettimeofday ();
  timer.schedule (&cb, &arg, initial, repeat);

  while (1)      // Don't let the main thread exit.
    ACE_OS::sleep (2);
  ACE_NOTREACHED (return 0);     // Not reached.
}
// Listing 2