Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Processes / Process_Mutex.cpp
blobe8ae41e3e178b8999e98c0c81aa082ac74d97f8d
1 #include "ace/OS_NS_unistd.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Process.h"
4 #include "ace/Process_Mutex.h"
6 // Listing 1 code/ch10
7 class GResourceUser
9 public:
10 GResourceUser (ACE_Process_Mutex &mutex) : gmutex_(mutex)
12 ACE_TRACE ("GResourceUser::GResourceUser");
15 void run ()
17 ACE_TRACE ("GResourceUser::run");
19 int count = 0;
20 while (count++ < 10)
22 int result = this->gmutex_.acquire ();
23 ACE_TEST_ASSERT (result == 0);
25 ACE_DEBUG ((LM_DEBUG,
26 ACE_TEXT ("(%P| %t) has the mutex\n")));
28 // Access Global resource
29 ACE_OS::sleep (1);
31 result = this->gmutex_.release ();
32 ACE_TEST_ASSERT (result == 0);
33 ACE_OS::sleep (1); // Give other process a chance.
37 private:
38 ACE_Process_Mutex &gmutex_;
40 // Listing 1
42 // Listing 0 code/ch10
43 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
45 if (argc > 1) // Run as the child.
47 // Create or get the global mutex.
48 ACE_Process_Mutex mutex ("GlobalMutex");
50 GResourceUser acquirer (mutex);
51 acquirer.run ();
53 else // Run as the parent.
55 ACE_Process_Options options;
56 options.command_line (ACE_TEXT ("%s a"), argv[0]);
57 ACE_Process processa, processb;
59 pid_t pida = processa.spawn (options);
60 pid_t pidb = processb.spawn (options);
62 ACE_DEBUG ((LM_DEBUG,
63 ACE_TEXT ("Spawned processes; pids %d:%d\n"),
64 pida, pidb));
66 if (processa.wait() == -1)
67 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
68 ACE_TEXT ("processa wait")), -1);
70 if (processb.wait() == -1)
71 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
72 ACE_TEXT ("processb wait")), -1);
75 return 0;
77 // Listing 0