Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Threads / task_five.cpp
bloba999638939a8fb415f4a8d723260dc486e8f279f
2 //=============================================================================
3 /**
4 * @file task_five.cpp
6 * Stress testing thread creation and thread cancellation using
7 * ACE_Task.
9 * @author Author: Detlef Becker <Detlef.Becker@med.siemens.de>
11 //=============================================================================
14 #include "ace/OS_main.h"
15 #include "ace/Thread_Manager.h"
16 #include "ace/Task.h"
17 #include "ace/OS_NS_unistd.h"
19 static const int DEFAULT_TASKS = 100;
20 static const int DEFAULT_ITERATIONS = 10;
22 // Default stack size
23 static size_t default_stack_size =
24 #if defined (ACE_WIN32)
26 #else
27 8192;
28 #endif /* ACE_WIN32 */
29 u_int loop_count = 0;
30 u_int error_count = 0;
32 class Test_Task : public ACE_Task<ACE_SYNCH>
34 public:
35 Test_Task (ACE_Thread_Manager * = ACE_Thread_Manager::instance ());
36 ~Test_Task () {};
38 //FUZZ: disable check_for_lack_ACE_OS
39 int open (void * = 0);
40 int svc ();
41 int close (u_long);
43 ///FUZZ: enable check_for_lack_ACE_OS
44 int shutdown ();
46 int synch ();
49 Test_Task::Test_Task (ACE_Thread_Manager *thrmgr)
50 : ACE_Task<ACE_SYNCH> (thrmgr)
54 int
55 Test_Task::open (void *)
57 return this->activate (0,
60 ACE_DEFAULT_THREAD_PRIORITY,
61 -1,
65 &default_stack_size);
68 int
69 Test_Task::svc ()
71 while (thr_mgr_->testcancel (ACE_OS::thr_self ()) == 0)
72 // Sleep for 350 msecs.
73 ACE_OS::sleep (ACE_Time_Value (0, 350000));
75 return 0;
78 int
79 Test_Task::close (u_long)
81 ACE_DEBUG ((LM_DEBUG, "(%t) closing down\n"));
82 return 0;
85 int
86 Test_Task::shutdown ()
88 return thr_mgr_->cancel_grp (grp_id_);
91 int
92 Test_Task::synch ()
94 return thr_mgr_->wait_grp (grp_id_);
97 static void
98 work (ACE_Thread_Manager *thr_mgr,
99 int n_tasks,
100 size_t stack_size)
102 ACE_UNUSED_ARG (stack_size);
104 int i;
105 Test_Task *task_array;
107 ACE_NEW (task_array,
108 Test_Task[n_tasks]);
110 ACE_DEBUG ((LM_DEBUG,
111 "Opening Tasks, loop count = %d, error count = %d\n",
112 loop_count,
113 error_count));
115 for (i = 0;
116 i < n_tasks;
117 i++)
118 task_array[i].open ();
120 ACE_OS::sleep (1);
122 ACE_DEBUG ((LM_DEBUG,
123 "Cancelling Tasks, loop count = %d, error count = %d\n",
124 loop_count,
125 error_count));
127 for (i = 0; i < n_tasks; i++)
128 task_array[i].shutdown ();
130 ACE_DEBUG ((LM_DEBUG,
131 "Synching Tasks, loop count = %d, error count = %d\n",
132 loop_count,
133 error_count));
135 for (i = 0;
137 i < n_tasks; i++)
138 if (-1 == task_array[i].synch ())
140 ACE_ERROR ((LM_ERROR,
141 "Error in synch! loop count = %d, error count = %d\n",
142 loop_count,
143 error_count));
144 error_count++;
147 ACE_DEBUG ((LM_DEBUG,
148 "thr_mgr->wait ();! loop count = %d, error count = %d\n",
149 loop_count,
150 error_count));
152 // Wait for all the threads to finish.
153 thr_mgr->wait ();
155 delete [] task_array;
156 loop_count++;
160 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
162 size_t stack_size = argc > 1 ? ACE_OS::atoi (argv[1]) : default_stack_size;
163 const int n_tasks = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_TASKS;
164 u_int iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : DEFAULT_ITERATIONS;
166 for (u_int i = 0; i < iterations; i++)
167 work (ACE_Thread_Manager::instance (),
168 n_tasks,
169 stack_size);
171 return 0;