summaryrefslogtreecommitdiff
path: root/docs/tutorials/016/page01.html
blob: 25e2ea1c78bea1074168ad86fa94f4d57794975b (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
85
<!-- $Id$ -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 016</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

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

<CENTER><B><FONT SIZE=+2>Making ACE_Condition easier to use</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
The ACE framework has quite a few objects for syncronizing your
threads and even processes.  We've mentioned a few in passing already:
 ACE_Thread_Mutex and ACE_Barrier for instance.
<P>
Another interesting one is the ACE_Condition template.  By using an
ACE_Condition you can have your code wait for an arbitrary condition
to occur.  That condition is "embodied" in a variable of your choice.
That variable can, in turn, be any data type you wish.  This makes
ACE_Condition much more flexible than a simple mutex, barrier or
semaphore.
<P>
In this tutorial, I'll create a wrapper class around the ACE_Condition
and the assorted housekeeping items necessary to make it work.  I'll
use a simple integer as the condition variable but keep in mind that
you can use any data type you want.
<P>
Kirthika's abstract:
<ul>
An ACE_Condition class is a synchronisation mechanism employed in
situations where one or more threads cannot access the shared resource
unless some 'condition' becomes true. The ACE_Condition is associated
with a Mutex-lock which is released before blocking internally in the
wait call. Once the blocked thread is signaled to wake up again it
internally re-acquires the lock before checking the condition.
Unless the condition is true and it has the lock, it cant go ahead.
Once the shared resource is freed, a signal is sent to the waiting
threads which can now contend for the lock and access the resource.
<P>
Pizza-delivery metaphor: (courtesy Dr.Schmidt)
<ul>
Pizza delivery boy --  thread<br>
keys to delivery van -- mutex-lock<br>
pizza ready for delivery -- condition<br>
</Ul>
Situation: <br>
<ul>
Five pizza delivery boys use the same van. While the van is
unavailable, the boys go to sleep. When the van returns and the keys
are returned, the current user of the van nudges the other sleeping boys
to wake up and fight for the keys. Once the keys are obtained and the
pizza
is ready and out of the kitchen, the boy with the pizza and the keys can
now deliver it.
</ul>
<P>
This tutorial makes use of  a wrapper class around the ACE_Condition and
uses a integer value as the condition. The four kinds of condition
implemented
are: !=, >=, <=, == by using C++ operator overloading. Guards are used
within
the methods to make sure that the method is thread-safe. Once the thread
completes
its job, it broadcasts to the waiting on it.
<P>
An ACE_Task is used to stress test the various conditions.
A test object is created for each condition and the main thread waits
until the condition becomes true. For instance: <= condition:
Five threads are spwaned which in turn increment the condition
variable, which is initialised to 1 by 2. Remember that you are waiting
on
the <= condition. So once 3 threads have been thru it, the value becomes
6
and the condition becomes true!
<P>
This simple example shows us how to program and use the Condition
variable for
synchronisation.
</ul>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page02.html">Continue This Tutorial</A>]</CENTER>