Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > 21280410b6ea906d791d7a12afae2579 > files > 285

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

<!-- page03.html,v 1.8 2000/03/19 20:09:31 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 016</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

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

<CENTER><B><FONT SIZE=+2>Making ACE_Condition easier to use</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
Ok, now we'll take a look at the definition of the class.  You already
know how to use an ACE_Condition & it's not really that difficult.
Still, imagine how much more cluttered your code would be if it had to
include the mess I've got below!
<HR>
<PRE>

<font color=red>// page03.html,v 1.8 2000/03/19 20:09:31 jcej Exp</font>

<font color=red>// Get or declaration</font>
<font color=blue>#include</font> "<font color=green>Condition_i.h</font>"

<font color=red>/* Initialize the condition variable and create the condition mutex.
   Since I don't have any guarantees on the order of member variable
   initialization, I have to new the condition mutex instead of
   simply constructing it.
 */</font>
<font color=#008888>Condition::Condition</font>(value_t _value)
        : value_(_value)
{
    condition_ = new condition_t( this->mutex() );
}

<font color=#008888>Condition::~Condition</font>(void)
{
     <font color=red>// Be sure we don't have a memeory leak</font>
    delete condition_;
}

<font color=red>/* The cast operator is the easiest way to return a copy of the value
   to clients of the class.  It also allows us to use a private method
   for getting a reference to the value when we need to modify it.
 */</font>
<font color=#008888>Condition::operator</font> Condition::value_t (void)
{
     <font color=red>// Place a guard around the variable so that it won't change as</font>
     <font color=red>// we're copying it back to the client.</font>
    guard_t    guard(mutex_);
    return value();
}

<font color=red>/* Traditional prefix increment operator.
   We place a guard around the operation so that we don't collide with
   any other threads.  After the modification, we broadcast() a
   condition change to any waiting threads.  You can also use signal()
   but that will only tell one thread about the change.  If that
   thread, in turn, invokes signal() then all threads will eventually
   find out.  I just thought it would be easier to use broadcast() and
   be done with it.
 */</font>
Condition & <font color=#008888>Condition::operator</font>++ (void)
{
    guard_t    guard(mutex_);

    ++value();

    condition().broadcast();

    return *this;
}

<font color=red>/* The remaining operators all follow the same pattern that we have
   above.  They only differ in the modification they make to the value().
 */</font>

Condition & <font color=#008888>Condition::operator</font>-- (void)
{
    guard_t    guard(mutex_);

    --value();

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>+= (int _inc)
{
    guard_t    guard(mutex_);

    value() += _inc;

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>-= (int _inc)
{
    guard_t    guard(mutex_);

    value() -= _inc;

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>*= (int _inc)
{
    guard_t    guard(mutex_);

    value() *= _inc;

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>/= (int _inc)
{
    guard_t    guard(mutex_);

    value() /= _inc;

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>%= (int _inc)
{
    guard_t    guard(mutex_);

    value() %= _inc;

    condition().broadcast();

    return *this;
}

Condition & <font color=#008888>Condition::operator</font>= ( value_t _value )
{
    guard_t    guard(mutex_);

    value() = _value;

    condition().broadcast();

    return *this;
}

<font color=red>/* Now we get into the comparison area.
   Each one follows the pattern we've already established for
   waiters.
 */</font>

<font color=red>/*
   We begin with an equality operator that expects a function object.
   In the while() test we pass a copy of the value to the function
   object for evaluation.  The object can then do any comparision it
   wants to check for a desired condition.  When the function object
   returns non-zero, the condition is met and we leave.
 */</font>
int <font color=#008888>Condition::operator</font>== ( Condition::Compare & _compare )
{
    guard_t    guard(mutex_);

    while( ! _compare(this->value()) )
        condition().wait();

    return 0;
}

<font color=red>// As long as the variable equals _value, we wait...</font>
int <font color=#008888>Condition::operator</font>== ( value_t _value )
{
    guard_t    guard(mutex_);

    while( value() == _value )
        condition().wait();

    return 0;
}

<font color=red>// As long as the variable is not equal to _value, we wait...</font>
int <font color=#008888>Condition::operator</font>!= ( value_t _value )
{
    guard_t    guard(mutex_);

    while( value() != _value )
        condition().wait();

    return 0;
}

<font color=red>// As long as the variable is less than or equal to _value, we wait...</font>
int <font color=#008888>Condition::operator</font>&lt;= ( value_t _value )
{
    guard_t    guard(mutex_);

    while( value() &lt;= _value )
        condition().wait();

    return 0;
}

<font color=red>// As long as the variable is greater than or equal to _value, we wait...</font>
int <font color=#008888>Condition::operator</font>>= ( value_t _value )
{
    guard_t    guard(mutex_);

    while( value() >= _value )
        condition().wait();

    return 0;
}
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page04.html">Continue This Tutorial</A>]</CENTER>