Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadSafety / Tokens.cpp
blob93765c89c1c72967748c4b6aa7191d9d6e6fce9d
1 #include "ace/Local_Tokens.h"
2 #include "ace/Token_Manager.h"
3 #include "ace/Task.h"
4 #include "ace/OS_NS_time.h"
6 #if defined (ACE_HAS_TOKENS_LIBRARY)
8 class Device;
10 // Listing 1 code/ch14
11 class HA_Device_Repository
13 public:
14 enum { N_DEVICES = 100 };
16 HA_Device_Repository ()
18 for (int i = 0; i < N_DEVICES; i++)
19 tokens_[i] = new ACE_Local_Mutex (0, 0, 1);
22 ~HA_Device_Repository ()
24 for (int i = 0; i < N_DEVICES; i++)
25 delete tokens_[i];
28 int update_device (int device_id, char *commands)
30 this->tokens_[device_id]->acquire ();
32 Device *curr_device = this->devices_[device_id];
33 internal_do (curr_device);
35 this->tokens_[device_id]->release ();
37 return 0;
40 void internal_do (Device *device);
42 private:
43 Device *devices_[N_DEVICES];
44 ACE_Local_Mutex *tokens_[N_DEVICES];
45 unsigned int seed_;
47 // Listing 1
49 void
50 HA_Device_Repository::internal_do (Device *device)
52 ACE_UNUSED_ARG (device); // Real code would use this.
53 ACE_Time_Value tv (0, ACE_OS::rand_r (&this->seed_) % 10000);
54 timespec_t t = (timespec_t)tv;
55 ACE_OS::nanosleep (&t);
58 // Listing 2 code/ch14
59 class HA_CommandHandler : public ACE_Task_Base
61 public:
62 enum { N_THREADS = 5 };
64 HA_CommandHandler (HA_Device_Repository &rep) : rep_(rep)
65 { }
67 int svc ()
69 for (int i = 0; i < HA_Device_Repository::N_DEVICES; i++)
70 rep_.update_device (i, "");
71 return 0;
74 private:
75 HA_Device_Repository &rep_;
78 int ACE_TMAIN (int, ACE_TCHAR *[])
80 HA_Device_Repository rep;
81 HA_CommandHandler handler (rep);
82 handler.activate (THR_NEW_LWP | THR_JOINABLE,
83 HA_CommandHandler::N_THREADS);
84 handler.wait ();
85 return 0;
87 // Listing 2
89 #else /* defined (ACE_HAS_TOKENS_LIBRARY) */
91 int ACE_TMAIN (int, ACE_TCHAR *[])
93 ACE_ERROR ((LM_ERROR,
94 ACE_TEXT ("local tokens not supported ")
95 ACE_TEXT ("on this platform\n")));
96 return 0;
99 #endif /* defined (ACE_HAS_TOKENS_LIBRARY) */