summaryrefslogtreecommitdiff
path: root/docs/tutorials/007/page01.html
blob: c8aacc2575274998b77069102d52396d9c180982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!-- $Id$ -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (X11; I; Linux 2.0.32 i486) [Netscape]">
   <META NAME="Author" CONTENT="James CE Johnson">
   <META NAME="Description" CONTENT="A first step towards using ACE productively">
   <TITLE>ACE Tutorial 007</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 007</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>Creating a thread-pool server</FONT></B></CENTER>
<HR>



<P>In this tutorial, we're going to extend Tutorial 6 to add a third concurrency
strategy:&nbsp; thread-pool.&nbsp; Like Tutorial 6 did to Tutorial 5, we're
going to keep the existing strategies that we've already created and add
this one in as a "bonus".&nbsp; As you'll see, our basic objects will change
but not by a whole lot.&nbsp; To accomplish this, we'll introduce one new
major object that helps to abstract the thread pool concept.
<P>
Some folks have noted that this tutorial is a bit confusing if you
don't first know about ACE_Task.  My advice is to give it all a good
read a couple of times.  If you're still having problems, take a look
at the ACE_Task tests in $ACE_ROOT/tests or examples in $ACE_ROOT/examples.
<P>
Kirthika's Abstract:
<UL>
In this multithreaded server, the Client_Acceptor has the additional
strategy of managing a thread pool. This helps when two clients don't
want to share the same resources or when different clients
need to run in different priority threads. We could then pool all the
same priority clients into one thread-pool. The thread_pool class is a
new addition used to implement this strategy. It inherits from ACE_Task
with ACE_MT_SYNCH parameter which takes care of syncronization issues
amongst multiple threads.
<P>
ACE_Task follows the Active Object pattern and executes the methods on
the task object in a new thread of execution, i.e it decouples the
execution of a method from its invocation. An ACE_Task has an underlying
thread (or pool of threads) and a Message_Queue which is the only means
of communication among tasks. A Message_Queue consists of
Message_Blocks.
<P>
The Client_Acceptor is registered with the reactor waiting for
connection requests.
On some activity, the reactor calls the handle_input() method on the
Acceptor. The Client_Handler of the Acceptor (for the thread-pool
strategy) unregisters itself from the reactor and
enqueues itself in the Message_Queue of the thread-pool  waiting for
svc() to call handle_input() on it. It would then process the data in
its new thread of execution. The ACE_MT_SYNCH option facilitates access
of the Mesage_Blocks across different Message_Queues (here from the main
thread to the one in the thread pool).
<P>
The thread_pool class derives from the ACE_Task class. Its svc() method
dequeues the threads in the Message_Queue and calls handle_input() on
them. The idle threads can take work from the queue while the other
threads are working. It also uses ACE_Atomic_Op as a counter for active
threads in the pool. Also, the ACE_Guard class has been used to provide
thread-safe counter-incrementation and destruction of the Message_Blocks
of the thread-pool. This class guards the critical section region by
acquiring the mutex lock on creation and releasing it as soon as it goes
out of scope.
<P>
Note: a sleep period before all the threads in the pool exit is
necessary for complete destruction of the thread pool.
<P>
This tutorial gives us a flavour of implementing a server with a
thread-pool strategy and how it can be managed using the ACE_Task class,
which provides an OO approach to thread-creation and implementation.
</UL>
<font size=-1>* The additions to this tutorial make use of
ACE_Message_Queue which is discussed in depth in
<A HREF="../010/page01.html">Tutorial 10</A>.  Feel free to read ahead
if you get lost in the message queue stuff.
</font>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page02.html">Continue This Tutorial</A>]</CENTER>