1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/OS_NS_string.h"
5 #include "ace/OS_NS_time.h"
8 #include "ace/SString.h"
10 // Listing 2 code/ch16
11 class Workers
: public ACE_Task
<ACE_MT_SYNCH
>
21 ACE_Message_Block
*mb
= 0;
22 if (this->getq (mb
) == -1)
25 ACE_TEXT ("(%t) Shutting down\n")));
29 // Process the message.
38 void process_message (ACE_Message_Block
*mb
)
40 ACE_TRACE ("Workers::process_message");
42 ACE_OS::memcpy (&msgId
, mb
->rd_ptr (), sizeof(int));
46 ACE_TEXT ("(%t) Started processing message %d\n"),
50 ACE_TEXT ("(%t) Finished processing message %d\n"),
55 // Listing 1 code/ch16
56 class Manager
: public ACE_Task
<ACE_MT_SYNCH
>
59 enum {POOL_SIZE
= 5, MAX_TIMEOUT
= 5};
61 Manager () : shutdown_(0)
63 ACE_TRACE ("Manager::Manager");
68 ACE_TRACE ("Manager::svc");
70 ACE_DEBUG ((LM_INFO
, ACE_TEXT ("(%t) Manager started\n")));
74 pool
.activate (THR_NEW_LWP
| THR_JOINABLE
, POOL_SIZE
);
78 ACE_Message_Block
*mb
= 0;
79 ACE_Time_Value
tv ((long)MAX_TIMEOUT
);
80 tv
+= ACE_OS::time (0);
82 // Get a message request.
83 if (this->getq (mb
, &tv
) < 0)
85 pool
.msg_queue ()->deactivate ();
90 // Ask the worker pool to do the job.
106 return (shutdown_
== 1);
110 int ACE_TMAIN (int, ACE_TCHAR
*[])
115 // Wait for a moment every time you send a message.
119 ACE_Message_Block
*mb
= 0;
120 for (int i
= 0; i
< 30; i
++)
123 (mb
, ACE_Message_Block(sizeof(int)), -1);
125 ACE_OS::memcpy (mb
->wr_ptr (), &i
, sizeof(int));
129 // Add a new work item.
133 ACE_Thread_Manager::instance ()->wait ();
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 */