summaryrefslogtreecommitdiff
path: root/docs/tutorials/010/page04.html
blob: c4d7c5706c744b39af0cc73ce9886c5e6f54b538 (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
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 010</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

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

<CENTER><B><FONT SIZE=+2>Passing chunks of data through an ACE_Message_Queue</FONT></B></CENTER>


<P>
<HR WIDTH="100%">
Our <A HREF="task.h">Task</A> object executes in one or more threads
and reads from the message queue it contains.
<P>

<HR WIDTH="100%">
<PRE>

<font color=red>// $Id$</font>

<font color=blue>#ifndef</font> <font color=purple>TASK_H</font>
<font color=blue>#define</font> <font color=purple>TASK_H</font>

<font color=blue>#include</font> "<font color=green>ace/Task.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=red>/*
  Like the thread-pool server tutorial, we'll derive from ACE_Task&lt;>.
  Our goal here is to show off the ACE_Message_Queue and the best way
  to do that is to use one to pass data between threads.  The easiest
  way to create threads is with ACE_Task&lt;>
 */</font>
class Task : public ACE_Task &lt; ACE_MT_SYNCH >
{
public:

  typedef ACE_Task &lt; ACE_MT_SYNCH > inherited;

    <font color=red>/*
      The constructor/destructor are simple but take care of some
      necessary housekeeping.
    */</font>
    Task (void);
   ~Task (void);

  <font color=red>/*
    To make our Task&lt;> derivative look more like other ACE objects
    I've added an open() method.  It will take care of activate()ing
    the object.
  */</font>
  int open (int threads = 1);

  <font color=red>/*
    Our worker method
  */</font>
  int svc (void);

  <font color=red>/*
    All we'll do here is print a message to the user.
  */</font>
  int close (u_long flags = 0);

protected:
    <font color=red>/*
      Just to be clever, I'll use an ACE_Barrier to cause the threads
      to sync in svc() before doing any real work.
    */</font>
    ACE_Barrier *barrier_;
};

<font color=blue>#endif</font>
</PRE>
<HR WIDTH="100%">
<P>
The only thing here that we didn't see in the thread-pool server is the
ACE_Barrier.  The application logic really doesn't need it but it is a
handy way to synchronize the threads at the beginning of svc().  In testing
I found that if I didn't sync svc(), the first thread to get activated would
tend to get all of the messages before the other threads came alive.
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page05.html">Continue This Tutorial</A>]</CENTER>