summaryrefslogtreecommitdiff
path: root/docs/tutorials/012/page04.html
blob: b83dd9e6ca295294b6f4cb5c4d89c7d787a8dd0b (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<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>
/*
  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()
*/
int run_test (int iterations, int threads)
{
     /*
       Create the Task which is our thread pool for doing work
     */
  Task task;

  if (task.open (threads) == -1)
  {
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);
  }

   /*
     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.
   */
  ACE_OS::sleep (ACE_Time_Value (1));

  for (int i = 0; i < iterations; ++i)
  {
    /*
       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.
     */
    Work * data = new Work(i);

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

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

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

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

   /*
     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.
   */
  task.wait ();

  return (0);
}

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

   /*
     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
     "exiting" message below.
   */
  (void) run_test (iterations, threads);

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Application exiting\n"));

  exit (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>
<HR WIDTH="100%">
<P>
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page05.html">Continue
This Tutorial</A>]</CENTER>

</BODY>
</HTML>