summaryrefslogtreecommitdiff
path: root/examples/Threads/task_five.cpp
blob: ffafa101afb3b9d472eeb543fbb032f029e95e26 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// $Id$

// Stress testing thread creation and thread cancellation using
// ACE_Task.  
//
// Author: Detlef Becker <Detlef.Becker@med.siemens.de>

#include "ace/Service_Config.h"
#include "ace/Thread_Manager.h"
#include "ace/Task.h"

static const int DEFAULT_TASKS = 1000;

class Test_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
  Test_Task (ACE_Thread_Manager *thrmgr = ACE_Service_Config::thr_mgr ());
  ~Test_Task (void) {};

  int open (void * = 0);
  int svc (void);
  int close (u_long);
  int shutdown (void);
  int synch (void);
};

Test_Task::Test_Task (ACE_Thread_Manager *thrmgr)
  : ACE_Task<ACE_MT_SYNCH> (thrmgr)
{
}

int
Test_Task::open (void *)
{
   return this->activate ();
}

int
Test_Task::svc (void)
{
  while (thr_mgr_->testcancel (ACE_OS::thr_self ()) == 0)
    // Sleep for 350 msecs.
    ACE_OS::sleep (ACE_Time_Value (0, 350000));

  return 0;
}

int
Test_Task::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) closing down\n"));
  return 0;
}

int
Test_Task::shutdown (void)
{
  return thr_mgr_->cancel_grp (grp_id_);
}

int
Test_Task::synch (void)
{
  return thr_mgr_->wait_grp (grp_id_);
}

int
main (int argc, char *argv[])
{
  const int n_tasks = argc > 1 ? ACE_OS::atoi (argv[1]) : DEFAULT_TASKS;
  u_int loop_count = 0;
  u_int error_count = 0;

  ACE_Thread_Manager *thr_mgr = ACE_Service_Config::thr_mgr ();

  Test_Task *task_array;

  for (;;)
    {
      int i;
      task_array = new Test_Task[n_tasks];

      ACE_DEBUG ((LM_DEBUG,
                  "Opening Tasks, loop count = %d, error count = %d\n",
                  loop_count,
                  error_count));

      for (i = 0; i < n_tasks; i++)
        task_array[i].open ();

      ACE_OS::sleep (1);

      ACE_DEBUG ((LM_DEBUG,
                  "Cancelling Tasks, loop count = %d, error count = %d\n",
                  loop_count,
                  error_count));

      for (i = 0; i < n_tasks; i++)
        task_array[i].shutdown ();

      ACE_DEBUG ((LM_DEBUG,
                  "Synching Tasks, loop count = %d, error count = %d\n",
                  loop_count,
                  error_count));

      for (i = 0; i < n_tasks; i++)
        if (-1 == task_array[i].synch ())
          {
            ACE_ERROR ((LM_ERROR,
                        "Error in synch! loop count = %d, error count = %d\n",
                        loop_count,
                        error_count));
            error_count++;
          }

      ACE_DEBUG ((LM_DEBUG,
                  "thr_mgr->wait ();! loop count = %d, error count = %d\n",
                  loop_count,
                  error_count));

      // Wait for all the threads to finish.
      thr_mgr->wait ();

      delete [] task_array;
      loop_count++;
    }

  return 0;
}