Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / Threads / task_one.cpp
blobe2dab9b6369d3ecc06bdb93ac63a3dcefde2c224
1 // This test program illustrates how the ACE barrier synchronization
2 // mechanisms work in conjunction with the ACE_Task and the
3 // ACE_Thread_Manager. It is instructive to compare this with the
4 // test_barrier.cpp test to see how they differ.
6 #include "ace/OS_main.h"
7 #include "ace/Task.h"
8 #include "ace/Service_Config.h"
11 #if defined (ACE_HAS_THREADS)
13 #include "ace/Task.h"
14 #include "ace/Barrier.h"
16 class Barrier_Task : public ACE_Task<ACE_MT_SYNCH>
18 public:
19 Barrier_Task (ACE_Thread_Manager *thr_mgr,
20 int n_threads,
21 int n_iterations);
23 virtual int svc ();
24 // Iterate <n_iterations> time printing off a message and "waiting"
25 // for all other threads to complete this iteration.
27 private:
28 ACE_Barrier barrier_;
29 // Reference to the tester barrier. This controls each
30 // iteration of the tester function running in every thread.
32 int n_iterations_;
33 // Number of iterations to run.
36 Barrier_Task::Barrier_Task (ACE_Thread_Manager *thr_mgr,
37 int n_threads,
38 int n_iterations)
39 : ACE_Task<ACE_MT_SYNCH> (thr_mgr),
40 barrier_ (n_threads),
41 n_iterations_ (n_iterations)
43 // Create worker threads.
44 if (this->activate (THR_NEW_LWP, n_threads) == -1)
45 ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));
48 // Iterate <n_iterations> time printing off a message and "waiting"
49 // for all other threads to complete this iteration.
51 int
52 Barrier_Task::svc ()
54 // Note that the ACE_Task::svc_run() method automatically adds us to
55 // the Thread_Manager when the thread begins.
57 for (int iterations = 1;
58 iterations <= this->n_iterations_;
59 iterations++)
61 ACE_DEBUG ((LM_DEBUG, "(%t) in iteration %d\n", iterations));
63 // Block until all other threads have waited, then continue.
64 this->barrier_.wait ();
67 // Note that the ACE_Task::svc_run() method automatically removes us
68 // from the Thread_Manager when the thread exits.
70 return 0;
73 // Default number of threads to spawn.
74 static const int DEFAULT_ITERATIONS = 5;
76 int
77 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
79 int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_THREADS;
80 int n_iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS;
82 Barrier_Task barrier_task (ACE_Thread_Manager::instance (),
83 n_threads,
84 n_iterations);
86 // Wait for all the threads to reach their exit point.
87 ACE_Thread_Manager::instance ()->wait ();
89 ACE_DEBUG ((LM_DEBUG, "(%t) done\n"));
90 return 0;
92 #else
93 int
94 ACE_TMAIN (int, ACE_TCHAR *[])
96 ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
97 return 0;
99 #endif /* ACE_HAS_THREADS */