Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / Threads / thread_manager.cpp
blob408ca76f53cea7f636deb8bdd9bbdaacced509de
1 // Test out the group management mechanisms provided by the
2 // ACE_Thread_Manager, including the group signal handling, group
3 // suspension and resumption, and cooperative thread cancellation
4 // mechanisms.
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_main.h"
8 #include "ace/Service_Config.h"
9 #include "ace/Thread_Manager.h"
10 #include "ace/Signal.h"
13 #if defined (ACE_HAS_THREADS)
15 extern "C" void
16 handler (int signum)
18 ACE_DEBUG ((LM_DEBUG, "(%t) received signal %d\n", signum));
21 static void *
22 worker (intptr_t iterations)
24 for (intptr_t i = 0; i < iterations; i++)
26 if ((i % 1000) == 0)
28 ACE_DEBUG ((LM_DEBUG,
29 "(%t) checking cancellation before iteration %d!\n",
30 i));
32 if (ACE_Thread_Manager::instance ()->testcancel (ACE_Thread::self ()) != 0)
34 ACE_DEBUG ((LM_DEBUG,
35 "(%t) has been cancelled before iteration %d!\n",
36 i));
37 break;
42 // Destructor removes thread from Thread_Manager.
43 return 0;
46 static const int DEFAULT_THREADS = ACE_DEFAULT_THREADS;
47 static const intptr_t DEFAULT_ITERATIONS = 100000;
49 int
50 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
52 ACE_Service_Config daemon;
54 daemon.open (argv[0]);
56 // Register a signal handler.
57 ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT);
58 ACE_UNUSED_ARG (sa);
60 int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : DEFAULT_THREADS;
61 intptr_t n_iterations =
62 argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS;
64 ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();
66 int grp_id = thr_mgr->spawn_n (n_threads,
67 ACE_THR_FUNC (worker),
68 reinterpret_cast<void *> (n_iterations),
69 THR_NEW_LWP | THR_DETACHED);
71 // Wait for 1 second and then suspend every thread in the group.
72 ACE_OS::sleep (1);
73 ACE_DEBUG ((LM_DEBUG, "(%t) suspending group\n"));
74 if (thr_mgr->suspend_grp (grp_id) == -1)
75 ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "suspend_grp"));
77 // Wait for 1 more second and then resume every thread in the
78 // group.
79 ACE_OS::sleep (ACE_Time_Value (1));
80 ACE_DEBUG ((LM_DEBUG, "(%t) resuming group\n"));
81 if (thr_mgr->resume_grp (grp_id) == -1)
82 ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "resume_grp"));
84 // Wait for 1 more second and then send a SIGINT to every thread in
85 // the group.
86 ACE_OS::sleep (ACE_Time_Value (1));
87 ACE_DEBUG ((LM_DEBUG, "(%t) signaling group\n"));
88 if (thr_mgr->kill_grp (grp_id, SIGINT) == -1)
89 ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "kill_grp"));
91 // Wait for 1 more second and then cancel all the threads.
92 ACE_OS::sleep (ACE_Time_Value (1));
93 ACE_DEBUG ((LM_DEBUG, "(%t) cancelling group\n"));
94 if (thr_mgr->cancel_grp (grp_id) == -1)
95 ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "cancel_grp"));
97 // Perform a barrier wait until all the threads have shut down.
98 thr_mgr->wait ();
99 return 0;
101 #else
103 ACE_TMAIN (int, ACE_TCHAR *[])
105 ACE_ERROR_RETURN ((LM_ERROR, "threads not supported on this platform\n"), -1);
107 #endif /* ACE_HAS_THREADS */