#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 1999-02-24 17:02 EST by <jcej@chiroptera.tragus.org>.
# Source directory was `/var/home/jcej/projects/ACE_wrappers/docs/tutorials/018'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode       name
# ------ ---------- ------------------------------------------
#    416 -rw-rw-r-- hdr
#     64 -rw-rw-r-- bodies
#   2603 -rw-rw-r-- page01.pre
#    430 -rw-rw-r-- page02.pre
#   1282 -rw-rw-r-- page03.pre
#    689 -rw-rw-r-- page04.pre
#    260 -rw-rw-r-- page05.pre
#   1523 -rw-rw-r-- page06.pre
#    478 -rw-rw-r-- page07.pre
#
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
  if test "$gettext_dir" = FAILED && test -f $dir/gettext \
     && ($dir/gettext --version >/dev/null 2>&1)
  then
    set `$dir/gettext --version 2>&1`
    if test "$3" = GNU
    then
      gettext_dir=$dir
    fi
  fi
  if test "$locale_dir" = FAILED && test -f $dir/shar \
     && ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
  then
    locale_dir=`$dir/shar --print-text-domain-dir`
  fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
  echo=echo
else
  TEXTDOMAINDIR=$locale_dir
  export TEXTDOMAINDIR
  TEXTDOMAIN=sharutils
  export TEXTDOMAIN
  echo="$gettext_dir/gettext -s"
fi
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
  shar_touch=touch
else
  shar_touch=:
  echo
  $echo 'WARNING: not restoring timestamps.  Consider getting and'
  $echo "installing GNU \`touch', distributed in GNU File Utilities..."
  echo
fi
rm -f 1231235999 $$.touch
#
if mkdir _sh30135; then
  $echo 'x -' 'creating lock directory'
else
  $echo 'failed to create lock directory'
  exit 1
fi
# ============= hdr ==============
if test -f 'hdr' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'hdr' '(file already exists)'
else
  $echo 'x -' extracting 'hdr' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'hdr' &&
<HTML>
<HEAD>
X   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
X   <META NAME="Author" CONTENT="James CE Johnson">
X   <TITLE>ACE Tutorial 018</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">
X
<CENTER><B><FONT SIZE=+2>ACE Tutorial 018</FONT></B></CENTER>
X
<CENTER><B><FONT SIZE=+2>The FIFO Nature of ACE_Token</FONT></B></CENTER>
X
<P>
<HR WIDTH="100%">
SHAR_EOF
  $shar_touch -am 1114141798 'hdr' &&
  chmod 0664 'hdr' ||
  $echo 'restore of' 'hdr' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'hdr:' 'MD5 check failed'
66dbcd27e23cdcc9c230089e9c289bcb  hdr
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'hdr'`"
    test 416 -eq "$shar_count" ||
    $echo 'hdr:' 'original size' '416,' 'current size' "$shar_count!"
  fi
fi
# ============= bodies ==============
if test -f 'bodies' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'bodies' '(file already exists)'
else
  $echo 'x -' extracting 'bodies' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'bodies' &&
PAGE=2
token.cpp
Test_T.h
Test_T.cpp
Token_i.h Mutex_i.h
output
SHAR_EOF
  $shar_touch -am 1114165298 'bodies' &&
  chmod 0664 'bodies' ||
  $echo 'restore of' 'bodies' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'bodies:' 'MD5 check failed'
22e70b25b6f23655b44d31fcf1a669f8  bodies
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'bodies'`"
    test 64 -eq "$shar_count" ||
    $echo 'bodies:' 'original size' '64,' 'current size' "$shar_count!"
  fi
fi
# ============= page01.pre ==============
if test -f 'page01.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page01.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page01.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page01.pre' &&
X
Welcome to Tutorial 18!
<P>
We've seen various ACE methods for synchronization in this and other
tutorial sections.  Something we haven't yet seen is the ACE_Token.
ACE_Token has a really cool thing:  it behaves in a FIFO manner.
<P>
Why is that cool?
<P>
In the other tutorials, you may have found that one thread will end up 
with all of the work.  Even though other threads are available, the OS 
scheduling and lock management just causes it to happen.  With
ACE_Token, the threads are queued up on the token and served in a
traditional first-in-first-out manner.
<P>
Why is FIFO important?
<P>
Well, if your app is running in a bunch of threads and each is doing
the same thing on the local host then FIFO may not be important.
However, take the case where each thread is connected to a remote
system.  Let's say you have a dozen threads in your app and each is
connected to a different remote system.  Each of the threads will be
given a block of data which will be passed to the remote for some
intense calculation.  If you use the FIFO then you'll spread the work
more-or-less evenly between the remote peers.  If you use the
traditional mutex then one peer may get the lion's share of the work.
<P>
It gets down to a personal decision based on the application's needs.
Consider your application, examine its behavior & decide for yourself
if you want to spread the work evenly or if it's OK to let some
threads work harder than others.
<P>
Kirthika's abstract:
<UL>
A token is similar to a mutex-lock, with the difference being that 
the token is given to the waiting threads in a FIFO order. In the case 
of the mutex-lock,  any thread (depending on the OS) could acquire 
the lock when its released. It internally implements a recursive mutex, 
i.e. the thread that owns the mutex can reqacquire it without deadlocking. 
The token also has two FIFO lists for writers and readers with writer- 
acquires having a higher priority than reader-acquires. 
<P>
This tutorial throws light on the differences on having a shared resource governed by 
a lock and a token, both derive from  a Task which simply updates a counter with the 
number-of-threads value. A barrier is used for ensuring that all threads get a equal 
opportunity of grabbing the token. The message queue with the message containing the 
thread count moves among the threads to be obtained and read. 
<P>
On obtaining the results, we conclude that on using the Token, the job to be completed 
can be distributed evenly among available threads. This cant be guaranteed 
in case of simply using the lock for synchronisation. 
</ul>
SHAR_EOF
  $shar_touch -am 0224170199 'page01.pre' &&
  chmod 0664 'page01.pre' ||
  $echo 'restore of' 'page01.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page01.pre:' 'MD5 check failed'
89cd84f46240da6d7d4d50e4d303e17a  page01.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page01.pre'`"
    test 2603 -eq "$shar_count" ||
    $echo 'page01.pre:' 'original size' '2603,' 'current size' "$shar_count!"
  fi
fi
# ============= page02.pre ==============
if test -f 'page02.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page02.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page02.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page02.pre' &&
Our main() just keeps getting simpler!  I guess that's a good thing.
<P>
What we've done is create two Task-derived objects that test different
locking mechanisms.  The Token object uses ACE_Token and the Mutex
object uses ACE_Mutex.  When you execute the application you should
see quite a difference in thread utilization.  At the end of the
tutorial I've included a link to the output of a typical run of the
application.
<HR>
SHAR_EOF
  $shar_touch -am 1114153698 'page02.pre' &&
  chmod 0664 'page02.pre' ||
  $echo 'restore of' 'page02.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page02.pre:' 'MD5 check failed'
a36353959f7874c8e31884d2acd7eb43  page02.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page02.pre'`"
    test 430 -eq "$shar_count" ||
    $echo 'page02.pre:' 'original size' '430,' 'current size' "$shar_count!"
  fi
fi
# ============= page03.pre ==============
if test -f 'page03.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page03.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page03.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page03.pre' &&
Our Test object is a simple derivative of ACE_Task.  Although we've
certainly used templates in other tutorials, this is the first time
we've created one of our own.
<P>
In a lot of ways, you can think of templates as the sophisticated
cousin of the old C-style pre-processor macros.  With templates,
however, you get better type-checking and much easier debugging.  There 
are certainly other benefits but these are my favorites.
<P>
Our template's MUTEX parameter is used to set the mutex type
<i>mutex_t</i>.  That'll be used in svc() so that we can protect
shared resources needed during the processing of data received on our
message queue.
<P>
Note at the bottom how we have to include the <i>cpp</i> file
associated with us.  Most compilers have to see the definition of
templated classes along with their declaration.  You might be tempted, 
therefore, to just put the definitions in the header file.  Resist
that temptation because templates are one of the fastest growing areas 
of compilers.  Including the definition like we do here leads to long
compile times and overly-large binaries.  With luck, the compilers
will get smarter in the future and we won't need definition
inclusion.  If you've already got them broken out then you'll save
yourself a lot of time!
<HR>
SHAR_EOF
  $shar_touch -am 1114154698 'page03.pre' &&
  chmod 0664 'page03.pre' ||
  $echo 'restore of' 'page03.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page03.pre:' 'MD5 check failed'
4f59a65824406215b1d28b2335b9c691  page03.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page03.pre'`"
    test 1282 -eq "$shar_count" ||
    $echo 'page03.pre:' 'original size' '1282,' 'current size' "$shar_count!"
  fi
fi
# ============= page04.pre ==============
if test -f 'page04.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page04.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page04.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page04.pre' &&
Our Test implementation looks much like the other ACE_Task derivatives 
we've used in the past.  The most obvious change is the addition of
the run() method.  run() will activate the threads and put a few
messages into the queue.  We could have done that in main() but it
just makes more sense here.
<P>
Notice how svc() grabs the guard after getting a message from the
queue.  Since we constructed our Task baseclass with ACE_MT_SYNCH, we
know that the queue is already thread-safe.  Our purpose in grabbing
the additional lock is to show how ACE_Token and ACE_Mutex behave
differently.  In a real app, you'd be doing this to protect shared
resources that the threads might clobber.
<HR>
SHAR_EOF
  $shar_touch -am 1114161298 'page04.pre' &&
  chmod 0664 'page04.pre' ||
  $echo 'restore of' 'page04.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page04.pre:' 'MD5 check failed'
428df54cf8483880bfcace604cd9ae3c  page04.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page04.pre'`"
    test 689 -eq "$shar_count" ||
    $echo 'page04.pre:' 'original size' '689,' 'current size' "$shar_count!"
  fi
fi
# ============= page05.pre ==============
if test -f 'page05.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page05.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page05.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page05.pre' &&
Here we create simple derivatives of our Test templated class.  Each
is parameterized with our mutex of choice and "named".  Using the Test 
template we're able to reuse all of the code with practially no
retyping and certainly much less chance of error!
<HR>
SHAR_EOF
  $shar_touch -am 1114161298 'page05.pre' &&
  chmod 0664 'page05.pre' ||
  $echo 'restore of' 'page05.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page05.pre:' 'MD5 check failed'
8f427c59ed060b06d99dfa81e0447454  page05.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page05.pre'`"
    test 260 -eq "$shar_count" ||
    $echo 'page05.pre:' 'original size' '260,' 'current size' "$shar_count!"
  fi
fi
# ============= page06.pre ==============
if test -f 'page06.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page06.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page06.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page06.pre' &&
That's it for the code, now let's take a quick look at some output.
The first set of output is from the Token test, the second is Mutex
test.  Notice how the threads are evenly utilized in the Token test.
Each thread gets to see exactly two messages.  There's also an
interesting side-effect that the messages are processed in order.  (You 
can't rely on that, it just happend in this particular run.)
<P>
With the Mutex test, however, we see that the first thread gets no
less than 1/2 of all messages.  In fact, if we didn't have the
governor in svc() it might have gotten them all!
<P>
Why does this happen?
<P>
Primarily because of time slicing.  Even though each thread takes time 
to do work (1 second in our test), it can still own the timeslice when 
it gets back to the mutex acquire.  Since the other threads are still
switched out, the current thread regets the lock and continues.  On
the other hand, the ACE_Token is very careful about the order in which 
the acquisition is allowed and more evenly distributes the work.
<P>
Play around with the sleep() call in svc().  You'll find that as you
decrease it, there is more chance that even the Token test will do
most of its work in one thread.  You're still at the mercy of the OS
time slicing.  In reality, though, it will take a moment or two for
work to be done.  The end goal isn't necessarily to distribute the
work evenly over all threads but, rather, to distribute it evenly
among <i>available</i> threads.  The distinction is subtle but important.
<HR>
SHAR_EOF
  $shar_touch -am 1114162298 'page06.pre' &&
  chmod 0664 'page06.pre' ||
  $echo 'restore of' 'page06.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page06.pre:' 'MD5 check failed'
9a4cc7f2ddce9c585a86113da4f5eb8d  page06.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page06.pre'`"
    test 1523 -eq "$shar_count" ||
    $echo 'page06.pre:' 'original size' '1523,' 'current size' "$shar_count!"
  fi
fi
# ============= page07.pre ==============
if test -f 'page07.pre' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'page07.pre' '(file already exists)'
else
  $echo 'x -' extracting 'page07.pre' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'page07.pre' &&
And now we're at the end of another Tutorial.  As always, feel free to 
send in questions and comments.  There are certainly more
implementation possibilites and I'll gladly integrate yours into these 
Tutorials.
<P>
<UL>
<LI><A HREF="Makefile">Makefile</A>
<LI><A HREF="token.cpp">token.cpp</A>
<LI><A HREF="Test.h">Test.h</A>
<LI><A HREF="Test.cpp">Test.cpp</A>
<LI><A HREF="Token_i.h">Token_i.h</A>
<LI><A HREF="Mutex_i.h">Mutex_i.h</A>
<LI><A HREF="output">output</A>
</UL>
SHAR_EOF
  $shar_touch -am 1114162598 'page07.pre' &&
  chmod 0664 'page07.pre' ||
  $echo 'restore of' 'page07.pre' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'page07.pre:' 'MD5 check failed'
e1a201541e51dd42654d31440eb4f36c  page07.pre
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page07.pre'`"
    test 478 -eq "$shar_count" ||
    $echo 'page07.pre:' 'original size' '478,' 'current size' "$shar_count!"
  fi
fi
rm -fr _sh30135
exit 0
