Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Threads / barrier1.cpp
blob6dea32ad377ff15e5ad9503e521bb0eb07945fd1
1 // This test program illustrates how the ACE barrier synchronization
2 // mechanisms work.
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)
12 struct Tester_Args
13 // = TITLE
14 // These arguments are passed into each test thread.
16 Tester_Args (ACE_Barrier &tb, int i)
17 : tester_barrier_ (tb),
18 n_iterations_ (i) {}
20 ACE_Barrier &tester_barrier_;
21 // Reference to the tester barrier. This controls each miteration of
22 // the tester function running in every thread.
24 int n_iterations_;
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.
31 static void *
32 tester (Tester_Args *args)
34 for (int iterations = 1;
35 iterations <= args->n_iterations_;
36 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 ();
44 return 0;
47 // Default number of threads to spawn.
48 static const int DEFAULT_ITERATIONS = 5;
50 int
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"));
71 return 0;
73 #else
74 int
75 ACE_TMAIN (int, ACE_TCHAR *[])
77 ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
78 return 0;
80 #endif /* ACE_HAS_THREADS */