diff options
Diffstat (limited to 'docs/tutorials/013/page03.html')
-rw-r--r-- | docs/tutorials/013/page03.html | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/docs/tutorials/013/page03.html b/docs/tutorials/013/page03.html deleted file mode 100644 index 43a2d0832ca..00000000000 --- a/docs/tutorials/013/page03.html +++ /dev/null @@ -1,124 +0,0 @@ -<HTML> -<HEAD> - <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> - <META NAME="Author" CONTENT="James CE Johnson"> - <TITLE>ACE Tutorial 013</TITLE> -</HEAD> -<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> - -<CENTER><B><FONT SIZE=+2>ACE Tutorial 013</FONT></B></CENTER> - -<CENTER><B><FONT SIZE=+2>Multiple thread pools</FONT></B></CENTER> - - -<P> -<HR WIDTH="100%"> -<P> -I did eventually create that ACE_Data_Block derivative that I wanted. -My purpose in doing so was to use the reference-counting -that is provided by ACE_Data_Block and ACE_Message_Block interactions. - When you're working with an object in a single -thread, it's generally not so difficult to manage it's lifetime. -That is, it doesn't tend to go out of scope or get destroyed unless -you do it on purpose. -<P> -On the other hand, if you're passing data between several threads, it -is easy to loose track of who "owns" the data at any one time. All -too frequently, data will be deleted by one thread while another is -still using it. Reference counting can prevent that. The rule of -thumb is that you increment the reference count of the object when you -hand it off to a new thread. You then decrement the count when you're -done with the object and let the object delete itself when there are -no more references. -<P> -To prove that all of that works correctly in the tutorial, I've -created a cheap Memory Leak Detector object. All mld instances -reference a thread-safe counter that is incremented when the mld is -constructed and decremented when destructed. I then insert an mld -into each of my dynamically created objects. If I get to the end of -main() and the counter isn't zero then I either didn't delete enough -or I deleted too many times. -<P> -Simple, cheap, effective. -<P> -<HR WIDTH="100%"> -<PRE> -<HR width=50%><P><center>mld.h</center><HR width=50%> - -<font color=red>// $Id$</font> - -<font color=blue>#ifndef</font> <font color=purple>MLD_H</font> -<font color=blue>#define</font> <font color=purple>MLD_H</font> - -<font color=blue>#include</font> "<font color=green>ace/Synch.h</font>" - -<font color=blue>#if !defined</font> (<font color=purple>ACE_LACKS_PRAGMA_ONCE</font>) -# pragma once -<font color=blue>#endif</font> <font color=red>/* ACE_LACKS_PRAGMA_ONCE */</font> - -<font color=blue>#include</font> "<font color=green>ace/Singleton.h</font>" - -<font color=red>/* - This is a cheap memory leak detector. Each class I want to watch over - contains an mld object. The mld object's ctor increments a global counter - while the dtor decrements it. If the counter is non-zero when the program - is ready to exit then there may be a leak. - */</font> - -class mld -{ -public: - mld (void); - ~mld (void); - - static int value (void); - -protected: - static ACE_Atomic_Op < ACE_Mutex, int >counter_; -}; - -<font color=red>// ================================================</font> - -<font color=red>/* - Just drop 'MLD' anywhere in your class definition to get cheap memory leak - detection for your class. - */</font> -<font color=blue>#define</font> <font color=purple>MLD</font> mld mld_ - -<font color=red>/* - Use 'MLD_COUNTER' in main() to see if things are OK. - */</font> -<font color=blue>#define</font> <font color=purple>MLD_COUNTER</font> <font color=#008888>mld::value</font>() - -<font color=red>// ================================================</font> - -<font color=blue>#endif</font> -</PRE> -<PRE> -<HR width=50%><P><center>mld.cpp</center><HR width=50%> - -<font color=red>// $Id$</font> - -<font color=blue>#include</font> "<font color=green>mld.h</font>" - -ACE_Atomic_Op < ACE_Mutex, int ><font color=#008888>mld::counter_</font> (0); - -<font color=red>// Increment the counter when a new mld is created...</font> -<font color=#008888>mld::mld</font> (void) -{ - ++counter_; -} - -<font color=red>// and decrement it when the object is destructed.</font> -<font color=#008888>mld::~mld</font> (void) -{ - --counter_; -} - -int <font color=#008888>mld::value</font> (void) -{ - return counter_.value (); -} -</PRE> -<P><HR WIDTH="100%"> -<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page04.html">Continue This Tutorial</A>]</CENTER> |