1 | // **********************************************************************
|
---|
2 | //
|
---|
3 | // Copyright (c) 2000
|
---|
4 | // Object Oriented Concepts, Inc.
|
---|
5 | // Billerica, MA, USA
|
---|
6 | //
|
---|
7 | // All Rights Reserved
|
---|
8 | //
|
---|
9 | // **********************************************************************
|
---|
10 |
|
---|
11 | #include <JTC/JTC.h>
|
---|
12 |
|
---|
13 | #include <stdlib.h>
|
---|
14 |
|
---|
15 | #ifdef HAVE_STD_IOSTREAM
|
---|
16 | using namespace std;
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | //
|
---|
20 | // This example is a port of the CubbyHole example in the java tutorial.
|
---|
21 | // See: http://java.sun.com/tutorial.
|
---|
22 | //
|
---|
23 | // This class acts as a cubby hole for producers and consumers.
|
---|
24 | // A producer may not put another item in the cubby until a consumer
|
---|
25 | // has retrieved the item. A consumer may not retrieve an item
|
---|
26 | // until a producer has placed a new item in the hole.
|
---|
27 | //
|
---|
28 | class CubbyHole
|
---|
29 | {
|
---|
30 | bool available_; // Item available?
|
---|
31 | int value_; // If so, what is the value of the item?
|
---|
32 | int ref_; // Number of times the cubby hole is referenced.
|
---|
33 | JTCMonitor mon_; // Associated monitor.
|
---|
34 |
|
---|
35 | public:
|
---|
36 |
|
---|
37 | //
|
---|
38 | // Constructor.
|
---|
39 | //
|
---|
40 | CubbyHole()
|
---|
41 | : available_(false), ref_(0)
|
---|
42 | {
|
---|
43 | }
|
---|
44 |
|
---|
45 | //
|
---|
46 | // Increment reference count.
|
---|
47 | //
|
---|
48 | void reference()
|
---|
49 | {
|
---|
50 | ++ref_;
|
---|
51 | }
|
---|
52 |
|
---|
53 | //
|
---|
54 | // Decrement reference count. Delete the cubby if the reference
|
---|
55 | // count drops to zero.
|
---|
56 | //
|
---|
57 | void dereference()
|
---|
58 | {
|
---|
59 | if(--ref_ == 0)
|
---|
60 | {
|
---|
61 | delete this;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | //
|
---|
66 | // Place a new item in the hole. If the hole is currently
|
---|
67 | // occupied, then block until the hole is empty.
|
---|
68 | //
|
---|
69 | void put(int v, int client)
|
---|
70 | {
|
---|
71 | JTCSynchronized sync(mon_);
|
---|
72 |
|
---|
73 | //
|
---|
74 | // Loop while the hole is filled.
|
---|
75 | //
|
---|
76 | while(available_)
|
---|
77 | {
|
---|
78 | //
|
---|
79 | // Wait to be notified. The monitor is notified when
|
---|
80 | // the hole is filled, or emptied.
|
---|
81 | //
|
---|
82 | try
|
---|
83 | {
|
---|
84 | mon_.wait();
|
---|
85 | }
|
---|
86 | catch(const JTCInterruptedException&)
|
---|
87 | {
|
---|
88 | }
|
---|
89 | }
|
---|
90 | //
|
---|
91 | // The hole is now filled, with associated value.
|
---|
92 | // Wake any waiting consumers.
|
---|
93 | //
|
---|
94 | available_ = true;
|
---|
95 | value_ = v;
|
---|
96 | mon_.notifyAll();
|
---|
97 |
|
---|
98 | //
|
---|
99 | // We put this display in here to avoid problems with
|
---|
100 | // corrupting the output stream.
|
---|
101 | //
|
---|
102 | cout << "Producer #" << client << " put: " << value_ << endl;
|
---|
103 | }
|
---|
104 |
|
---|
105 | //
|
---|
106 | // Retrieve an element store in the cubby hole. If no element
|
---|
107 | // is available for retrieval then wait.
|
---|
108 | //
|
---|
109 | int get(int client)
|
---|
110 | {
|
---|
111 | JTCSynchronized sync(mon_);
|
---|
112 | //
|
---|
113 | // While no element is available for retrieval wait.
|
---|
114 | //
|
---|
115 | while(!available_)
|
---|
116 | {
|
---|
117 | try
|
---|
118 | {
|
---|
119 | mon_.wait();
|
---|
120 | }
|
---|
121 | catch(const JTCInterruptedException&)
|
---|
122 | {
|
---|
123 | }
|
---|
124 | }
|
---|
125 | //
|
---|
126 | // Retrieve the element. Mark the state as "no element available".
|
---|
127 | // Notify any waiting producers.
|
---|
128 | //
|
---|
129 | available_ = false;
|
---|
130 | mon_.notifyAll();
|
---|
131 | //
|
---|
132 | // Display the value while the monitor is still locked, since
|
---|
133 | // this will prevent corruption of the output stream.
|
---|
134 | //
|
---|
135 | cout << "Consumer #" << client << " get: " << value_ << endl;
|
---|
136 |
|
---|
137 | return value_;
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 | //
|
---|
142 | // This class represents a consumer thread. This class is responsible
|
---|
143 | // for retrieving elements from the cubby hole.
|
---|
144 | //
|
---|
145 | class Consumer : public JTCThread
|
---|
146 | {
|
---|
147 | CubbyHole* cubby_; // Pointer to the cubby hole.
|
---|
148 | int number_; // The client number, for debugging.
|
---|
149 | public:
|
---|
150 | //
|
---|
151 | // Constructor.
|
---|
152 | //
|
---|
153 | Consumer(CubbyHole* c, int which)
|
---|
154 | : JTCThread("consumer"), cubby_(c), number_(which)
|
---|
155 | {
|
---|
156 | cubby_ -> reference();
|
---|
157 | }
|
---|
158 |
|
---|
159 | //
|
---|
160 | // Destructor.
|
---|
161 | //
|
---|
162 | virtual ~Consumer()
|
---|
163 | {
|
---|
164 | cubby_ -> dereference();
|
---|
165 | }
|
---|
166 |
|
---|
167 | //
|
---|
168 | // This method is called when start is invoked on the thread.
|
---|
169 | //
|
---|
170 | virtual void run()
|
---|
171 | {
|
---|
172 | //
|
---|
173 | // Retrieve 10 numbers from the cubby hole.
|
---|
174 | //
|
---|
175 | for(int i = 0; i < 10; ++i)
|
---|
176 | {
|
---|
177 | cubby_ -> get(number_);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | };
|
---|
181 |
|
---|
182 | //
|
---|
183 | // This class represents the producer thread. This thread places objects
|
---|
184 | // in the cubbyhole for a consumer to retrieve.
|
---|
185 | //
|
---|
186 | class Producer : public JTCThread
|
---|
187 | {
|
---|
188 | CubbyHole* cubby_; // The cubby hole object.
|
---|
189 | int number_; // The client number, for debugging.
|
---|
190 |
|
---|
191 | public:
|
---|
192 | //
|
---|
193 | // Constructor.
|
---|
194 | //
|
---|
195 | Producer(CubbyHole* c, int which)
|
---|
196 | : JTCThread("producer"), cubby_(c), number_(which)
|
---|
197 | {
|
---|
198 | cubby_ -> reference();
|
---|
199 | }
|
---|
200 |
|
---|
201 | //
|
---|
202 | // Destructor.
|
---|
203 | //
|
---|
204 | virtual ~Producer()
|
---|
205 | {
|
---|
206 | cubby_ -> dereference();
|
---|
207 | }
|
---|
208 |
|
---|
209 | //
|
---|
210 | // This method represents the main loop for this thread.
|
---|
211 | // It places 10 numbers in the cubby hole.
|
---|
212 | //
|
---|
213 | virtual void run()
|
---|
214 | {
|
---|
215 | for(int i = 0; i < 10; ++i)
|
---|
216 | {
|
---|
217 | cubby_ -> put(i, number_);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | };
|
---|
221 |
|
---|
222 | int
|
---|
223 | main(int argc, char** argv)
|
---|
224 | {
|
---|
225 | try
|
---|
226 | {
|
---|
227 | //
|
---|
228 | // A user of the JTC library must create an instance of this
|
---|
229 | // class to initialize the library.
|
---|
230 | //
|
---|
231 | JTCInitialize bootJTC(argc, argv);
|
---|
232 |
|
---|
233 | //
|
---|
234 | // Create an instance of the CubbyHole class.
|
---|
235 | //
|
---|
236 | CubbyHole* c = new CubbyHole;
|
---|
237 |
|
---|
238 | //
|
---|
239 | // Create the producer and the consumer.
|
---|
240 | //
|
---|
241 | JTCThreadHandle producer = new Producer(c, 0);
|
---|
242 | JTCThreadHandle consumer = new Consumer(c, 1);
|
---|
243 |
|
---|
244 | //
|
---|
245 | // Start the threads.
|
---|
246 | //
|
---|
247 | producer -> start();
|
---|
248 |
|
---|
249 | consumer -> start();
|
---|
250 | //
|
---|
251 | // The instance of the JTCInitialize class will ensure that
|
---|
252 | // this block is not exited until all threads terminate.
|
---|
253 | //
|
---|
254 | }
|
---|
255 | catch(const JTCException& e)
|
---|
256 | {
|
---|
257 | cerr << "JTCException: " << e.getMessage() << endl;
|
---|
258 | return EXIT_FAILURE;
|
---|
259 | }
|
---|
260 |
|
---|
261 | return EXIT_SUCCESS;
|
---|
262 | }
|
---|