summaryrefslogtreecommitdiff
path: root/examples/Threads/test_token.cpp
blob: 5a51496d011e2100cddeb8fef0dfe089bed0a4aa (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Test out the ACE Token class.
// $Id$

#include "ace/Token.h"
#include "ace/Task.h"

#if defined (ACE_HAS_THREADS)

class My_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
  My_Task (int n);
  virtual int open (void *) { return 0; }
  virtual int close (u_long) { return 0; }
  virtual int put (ACE_Message_Block *, ACE_Time_Value *) { return 0; }
  virtual int svc (void);

  static void sleep_hook (void *);

private:
  ACE_Token token_;
};

My_Task::My_Task (int n)
{
  // Make this Task into an Active Object.
  this->activate (THR_BOUND | THR_DETACHED, n);

  // Wait for all the threads to exit.
  this->thr_mgr ()->wait ();
}

void 
My_Task::sleep_hook (void *)
{
  cerr << '(' << ACE_Thread::self () << ')'
       << " blocking, My_Task::sleep_hook () called" << endl;
}

// Test out the behavior of the ACE_Token class.

int 
My_Task::svc (void)
{
  for (int i = 0; i < 10000; i++)
    {
      // Wait for up to 1 millisecond past the current time to get the token.
      ACE_Time_Value timeout (ACE_OS::time (0), 1000);

      if (this->token_.acquire (&My_Task::sleep_hook, 0, &timeout) == 1)
	{
	  this->token_.acquire ();
	  this->token_.renew ();
	  this->token_.release ();
	  this->token_.release ();
	}
      else
	ACE_Thread::yield ();
    }
  return 0;
}

int 
main (int argc, char *argv[])
{
  My_Task tasks (argc > 1 ? atoi (argv[1]) : 4);

  return 0;
}
#else
int 
main (void)
{
  ACE_ERROR_RETURN ((LM_ERROR, "your platform doesn't support threads\n"), -1);
}
#endif /* */