3 #include "ace/Log_Msg.h"
4 #include "ace/Atomic_Op.h"
6 #if defined(RUNNING_ON_UNSAFE_MULTIPROCESSOR)
8 typedef ACE_Atomic_Op
<ACE_Thread_Mutex
, unsigned int> SafeUInt
;
10 // Listing 2 code/ch14
11 typedef ACE_Atomic_Op
<ACE_Thread_Mutex
, int> SafeInt
;
14 typedef ACE_Atomic_Op
<ACE_Null_Mutex
, unsigned int> SafeUInt
;
15 typedef ACE_Atomic_Op
<ACE_Null_Mutex
, int> SafeInt
;
16 #endif /* RUNNING_ON_UNSAFE_MULTIPROCESSOR) */
18 static const unsigned int Q_SIZE
= 2;
19 static const int MAX_PROD
= 10;
21 // Listing 3 code/ch14
22 class Producer
: public ACE_Task_Base
25 Producer (int *buf
, SafeUInt
&in
, SafeUInt
&out
)
26 : buf_(buf
), in_(in
), out_(out
)
37 while (in_
.value () - out_
.value () == Q_SIZE
);
40 buf_
[in_
.value () % Q_SIZE
] = itemNo
.value ();
43 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Produced %d\n"),
46 if (check_termination (itemNo
.value ()))
53 int check_termination (int item
)
55 return (item
== MAX_PROD
);
64 class Consumer
: public ACE_Task_Base
67 Consumer (int *buf
, SafeUInt
&in
, SafeUInt
& out
)
68 : buf_(buf
), in_(in
), out_(out
)
80 while (in_
.value () - out_
.value () == 0);
82 item
= buf_
[out_
.value () % Q_SIZE
];
85 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Consumed %d\n"),
88 if (check_termination (item
))
95 int check_termination (int item
)
97 return (item
== MAX_PROD
);
107 // Listing 4 code/ch14
108 int ACE_TMAIN (int, ACE_TCHAR
*[])
110 int shared_buf
[Q_SIZE
];
114 Producer
producer (shared_buf
, in
, out
);
115 Consumer
consumer (shared_buf
, in
, out
);