Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Threads / recursive_mutex.cpp
blob522a4ce7bfa74d772a9a395e78840a01bf6d88f5
1 // This test program verifies the functionality of the ACE_OS
2 // implementation of recursive mutexes on Win32 and Posix pthreads.
4 #include "ace/OS_main.h"
5 #include "ace/Service_Config.h"
6 #include "ace/Thread_Manager.h"
7 #include "ace/Get_Opt.h"
10 #if defined (ACE_HAS_THREADS)
12 #include "ace/Guard_T.h"
13 #include "ace/Recursive_Thread_Mutex.h"
15 // Total number of iterations.
16 static size_t n_iterations = 1000;
17 static size_t n_threads = 4;
19 // Explain usage and exit.
20 static void
21 print_usage_and_die ()
23 ACE_DEBUG ((LM_DEBUG,
24 "usage: %n [-t n_threads] [-n iteration_count]\n"));
25 ACE_OS::exit (1);
28 // Parse the command-line arguments and set options.
30 static void
31 parse_args (int argc, ACE_TCHAR *argv[])
33 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("n:t:"));
35 int c;
37 while ((c = get_opt ()) != -1)
38 switch (c)
40 case 'n':
41 n_iterations = ACE_OS::atoi (get_opt.opt_arg ());
42 break;
43 case 't':
44 n_threads = ACE_OS::atoi (get_opt.opt_arg ());
45 break;
46 default:
47 print_usage_and_die ();
48 break;
52 static void
53 recursive_worker (size_t nesting_level,
54 ACE_Recursive_Thread_Mutex *rm)
56 if (nesting_level < n_iterations)
58 ACE_DEBUG ((LM_DEBUG,
59 "(%P|%t) = trying to acquire, nesting = %d, thread id = %u\n",
60 rm->get_nesting_level (), rm->get_thread_id ()));
62 // This illustrates the use of the ACE_GUARD with an
63 // ACE_Recursive_Thread_Mutex.
64 ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon, *rm);
65 ACE_DEBUG ((LM_DEBUG,
66 "(%P|%t) = acquired, nesting = %d, thread id = %u\n",
67 rm->get_nesting_level (), rm->get_thread_id ()));
69 recursive_worker (nesting_level + 1, rm);
71 ACE_DEBUG ((LM_DEBUG,
72 "(%P|%t) = released, nesting = %d, thread id = %u\n",
73 rm->get_nesting_level (), rm->get_thread_id ()));
77 static void *
78 worker (void *arg)
80 ACE_Recursive_Thread_Mutex *rm
81 = (ACE_Recursive_Thread_Mutex *) arg;
83 recursive_worker (0, rm);
84 return 0;
87 int
88 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
90 ACE_Service_Config daemon (argv[0]);
92 parse_args (argc, argv);
93 ACE_Recursive_Thread_Mutex rm;
95 ACE_Thread_Manager::instance ()->spawn_n (n_threads,
96 ACE_THR_FUNC (worker),
97 (void *) &rm);
99 ACE_Thread_Manager::instance ()->wait ();
100 return 0;
102 #else
104 ACE_TMAIN (int, ACE_TCHAR *[])
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "ACE doesn't support support process mutexes on this platform (yet)\n"),
108 -1);
110 #endif /* ACE_WIN32 */