Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > media > contrib > by-pkgid > 21280410b6ea906d791d7a12afae2579 > files > 182

libace5-doc-5.4-2mdk.i586.rpm

<!-- page04.html,v 1.10 2000/03/19 20:09:26 jcej Exp -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 012</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

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

<CENTER><B><FONT SIZE=+2>Passing classes through ACE_Message_Queue</FONT></B></CENTER>


<P>
<HR WIDTH="100%">
<P>
Ok, finally we get to main().  Sorry for the diversion but it was
important to lay some of that groundwork before getting here.
<P>
<HR WIDTH="100%">
<PRE>

<font color=red>// page04.html,v 1.10 2000/03/19 20:09:26 jcej Exp</font>

<font color=blue>#include</font> "<font color=green>block.h</font>"
<font color=blue>#include</font> "<font color=green>work.h</font>"
<font color=blue>#include</font> "<font color=green>task.h</font>"

<font color=red>/*
  I want to be sure that our Task object gets destructed correctly, so
  I'll do most of the application 'work' in run_test() instead of
  main()
*/</font>
int run_test (int iterations, int threads)
{
     <font color=red>/*
       Create the Task which is our thread pool for doing work
     */</font>
  Task task;

  if (task.start (threads) == -1)
  {
    ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>start</font>"), -1);
  }

   <font color=red>/*
     Give the Task a chance to enter it's svc() method.  This isn't
     really necessary and you probably wouldn't do it in a real
     application but it makes the output more interesting.
   */</font>
  <font color=#008888>ACE_OS::sleep</font> (ACE_Time_Value (1));

  for (int i = 0; i &lt; iterations; ++i)
  {
    <font color=red>/*
       Construct a Work object that we'll put into the Queue.  Give it
       the iteration number so that it can identify itself in the output.
     */</font>
    Work * data = new Work(i);

    <font color=red>/*
       Create a block that contains our Work object but also has
       enough room for a text message.
     */</font>
    Message_Block *message = new Message_Block (128, data);

    <font color=red>/*
       As before, put a text message into the block.
     */</font>
    <font color=#008888>ACE_OS::sprintf</font> (message->wr_ptr (), "<font color=green>This is message %d.</font>", i);
    message->wr_ptr (strlen (message->rd_ptr ())+1);

     <font color=red>/*
       Add the work to our thread pool
     */</font>
    if (task.putq (message) == -1)
    {
      break;
    }
  }

   <font color=red>/*
     Insert a HANGUP message block to tell the thread pool to shut
     itself down.
    */</font>
  Message_Block *message = new Message_Block (0,0);
  message->msg_type (<font color=#008888>ACE_Message_Block::MB_HANGUP</font>);
  task.putq (message);

   <font color=red>/*
     Wait for the all threads of the Task to exit.  It is rather rude
     to let the Task go out of scope without doing this first.
   */</font>
  task.wait ();

  return (0);
}

int main (int argc, char *argv[])
{
     <font color=red>/*
       Give the user a chance to override the default number of
       iterations and pool threads.
     */</font>
  int iterations = argc > 1 ? atoi (argv[1]) : 4;
  int threads = argc > 2 ? atoi (argv[2]) : 2;

   <font color=red>/*
     Use the function above to do the actual test.  As I said, this
     lets us see the Task go out of scope and destruct before our
     "<font color=green>exiting</font>" message below.
   */</font>
  (void) run_test (iterations, threads);

  ACE_DEBUG ((LM_DEBUG, "<font color=green>(%P|%t) Application exiting\n</font>"));

  return (0);
}
</PRE>
<HR WIDTH="100%">
<P>
That certainly looks cleaner than the previous approach!  If you
blink, you'll miss the part where the Work object goes into the Queue.
<P>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page05.html">Continue This Tutorial</A>]</CENTER>