1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/OS_NS_string.h"
9 class Consumer
: public ACE_Task
<ACE_MT_SYNCH
>
12 enum { N_THREADS
= 5 };
14 Consumer (ACE_Semaphore
& psema
, ACE_Semaphore
& csema
)
15 : psema_(psema
), csema_(csema
), exit_condition_(0)
30 ACE_Message_Block
*mb
= 0;
32 if (mb
->msg_type () == ACE_Message_Block::MB_HANGUP
)
41 ACE_TEXT ("(%t) Consumed %d\n"),
42 *((int*)mb
->rd_ptr ())));
49 //FUZZ: disable check_for_lack_ACE_OS
52 //FUZZ: enable check_for_lack_ACE_OS
54 this->msg_queue ()->deactivate ();
55 csema_
.release (N_THREADS
);
60 return exit_condition_
;
64 ACE_Semaphore
& psema_
;
65 ACE_Semaphore
& csema_
;
69 // Listing 1 code/ch14
70 class Producer
: public ACE_Task_Base
73 enum { MAX_PROD
= 128 };
75 Producer (ACE_Semaphore
& psema
, ACE_Semaphore
& csema
,
77 : psema_(psema
), csema_(csema
), consumer_(consumer
)
82 for (int i
= 0; i
<= MAX_PROD
; i
++)
88 void produce_item (int item
)
92 = new ACE_Message_Block (sizeof (int),
93 ACE_Message_Block::MB_DATA
);
94 ACE_OS::memcpy (mb
->wr_ptr (), &item
, sizeof item
);
95 mb
->wr_ptr (sizeof (int));
96 this->consumer_
.putq (mb
);
98 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) Produced %d\n"), item
));
105 ACE_Message_Block
*mb
=
106 new ACE_Message_Block (0, ACE_Message_Block::MB_HANGUP
);
107 this->consumer_
.putq (mb
);
112 ACE_Semaphore
& psema_
;
113 ACE_Semaphore
& csema_
;
117 // Listing 3 code/ch14
118 int ACE_TMAIN (int, ACE_TCHAR
*[])
120 ACE_Semaphore
psem (5);
121 ACE_Semaphore
csem (0);
123 Consumer
consumer (psem
, csem
);
124 Producer
producer (psem
, csem
, consumer
);
126 producer
.activate ();
127 consumer
.activate (THR_NEW_LWP
| THR_JOINABLE
,
128 Consumer::N_THREADS
);
138 #include "ace/OS_main.h"
139 #include "ace/OS_NS_stdio.h"
141 int ACE_TMAIN (int, ACE_TCHAR
*[])
143 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
147 #endif /* ACE_HAS_THREADS */