1 // This test program illustrates how the ACE barrier synchronization
4 #include "ace/OS_main.h"
5 #include "ace/Barrier.h"
6 #include "ace/Thread_Manager.h"
7 #include "ace/Service_Config.h"
10 #if defined (ACE_HAS_THREADS)
14 // These arguments are passed into each test thread.
16 Tester_Args (ACE_Barrier
&tb
, int i
)
17 : tester_barrier_ (tb
),
20 ACE_Barrier
&tester_barrier_
;
21 // Reference to the tester barrier. This controls each miteration of
22 // the tester function running in every thread.
25 // Number of iterations to run.
28 // Iterate <n_iterations> time printing off a message and "waiting"
29 // for all other threads to complete this iteration.
32 tester (Tester_Args
*args
)
34 for (int iterations
= 1;
35 iterations
<= args
->n_iterations_
;
38 ACE_DEBUG ((LM_DEBUG
, "(%t) in iteration %d\n", iterations
));
40 // Block until all other threads have waited, then continue.
41 args
->tester_barrier_
.wait ();
47 // Default number of threads to spawn.
48 static const int DEFAULT_ITERATIONS
= 5;
51 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
53 ACE_Service_Config
daemon (argv
[0]);
55 int n_threads
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : ACE_DEFAULT_THREADS
;
56 int n_iterations
= argc
> 2 ? ACE_OS::atoi (argv
[2]) : DEFAULT_ITERATIONS
;
58 ACE_Barrier
tester_barrier (n_threads
);
60 Tester_Args
args (tester_barrier
, n_iterations
);
62 if (ACE_Thread_Manager::instance ()->spawn_n
63 (int(n_threads
), ACE_THR_FUNC (tester
),
64 (void *) &args
, THR_NEW_LWP
| THR_DETACHED
) == -1)
65 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "spawn_n"), 1);
67 // Wait for all the threads to reach their exit point.
68 ACE_Thread_Manager::instance ()->wait ();
70 ACE_DEBUG ((LM_DEBUG
, "(%t) done\n"));
75 ACE_TMAIN (int, ACE_TCHAR
*[])
77 ACE_ERROR ((LM_ERROR
, "threads not supported on this platform\n"));
80 #endif /* ACE_HAS_THREADS */