Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Threads / Mutexes.cpp
blob29f8c195354a8eb56e96910df1aa58833c02bd73
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/Synch.h"
5 #include "ace/Task.h"
7 // Listing 1 code/ch12
8 class HA_Device_Repository
10 public:
11 HA_Device_Repository ()
12 { }
14 void update_device (int device_id)
16 mutex_.acquire ();
17 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Updating device %d\n"),
18 device_id));
19 ACE_OS::sleep (1);
20 mutex_.release ();
23 private:
24 ACE_Thread_Mutex mutex_;
26 // Listing 1
27 // Listing 2 code/ch12
28 class HA_CommandHandler : public ACE_Task_Base
30 public:
31 enum {NUM_USES = 10};
33 HA_CommandHandler (HA_Device_Repository& rep) : rep_(rep)
34 { }
36 virtual int svc ()
38 ACE_DEBUG
39 ((LM_DEBUG, ACE_TEXT ("(%t) Handler Thread running\n")));
40 for (int i=0; i < NUM_USES; i++)
41 this->rep_.update_device (i);
42 return 0;
45 private:
46 HA_Device_Repository & rep_;
49 int ACE_TMAIN (int, ACE_TCHAR *[])
51 HA_Device_Repository rep;
52 HA_CommandHandler handler1 (rep);
53 HA_CommandHandler handler2 (rep);
54 handler1.activate ();
55 handler2.activate ();
57 handler1.wait ();
58 handler2.wait ();
59 return 0;
61 // Listing 2
63 #else
64 #include "ace/OS_main.h"
65 #include "ace/OS_NS_stdio.h"
67 int ACE_TMAIN (int, ACE_TCHAR *[])
69 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
70 return 0;
73 #endif /* ACE_HAS_THREADS */