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"
8 #include "ace/Service_Config.h"
11 #if defined (ACE_HAS_THREADS)
14 #include "ace/Barrier.h"
16 class Barrier_Task
: public ACE_Task
<ACE_MT_SYNCH
>
19 Barrier_Task (ACE_Thread_Manager
*thr_mgr
,
24 // Iterate <n_iterations> time printing off a message and "waiting"
25 // for all other threads to complete this iteration.
29 // Reference to the tester barrier. This controls each
30 // iteration of the tester function running in every thread.
33 // Number of iterations to run.
36 Barrier_Task::Barrier_Task (ACE_Thread_Manager
*thr_mgr
,
39 : ACE_Task
<ACE_MT_SYNCH
> (thr_mgr
),
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.
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_
;
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.
73 // Default number of threads to spawn.
74 static const int DEFAULT_ITERATIONS
= 5;
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 (),
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"));
94 ACE_TMAIN (int, ACE_TCHAR
*[])
96 ACE_ERROR ((LM_ERROR
, "threads not supported on this platform\n"));
99 #endif /* ACE_HAS_THREADS */