📄 combine.shar
字号:
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 03191459100 '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 03191459100 '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'
68432fc61bf23fe391f4ead60fe26b17 page03.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page03.pre'`"
test 1279 -eq "$shar_count" ||
$echo 'page03.pre:' 'original size' '1279,' '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 03191459100 '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'
51ea4870a85f49c113951fedd695e35f page04.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page04.pre'`"
test 688 -eq "$shar_count" ||
$echo 'page04.pre:' 'original size' '688,' '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 03191459100 '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'
31b8531c267e036e6f463249cd04b53a page05.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page05.pre'`"
test 259 -eq "$shar_count" ||
$echo 'page05.pre:' 'original size' '259,' '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 03191459100 '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'
ac415a062a971ba79a088d929dda5f36 page06.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page06.pre'`"
test 1519 -eq "$shar_count" ||
$echo 'page06.pre:' 'original size' '1519,' '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 03191459100 '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'
b7e7273c9bbeec45bdf2df2e8af06323 page07.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page07.pre'`"
test 476 -eq "$shar_count" ||
$echo 'page07.pre:' 'original size' '476,' 'current size' "$shar_count!"
fi
fi
rm -fr _sh00359
exit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -