Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-07-23 05:50:09


From: "Jason Winnebeck" <yg-boost-users_at_[hidden]>
[...]
> > //waitForPlayer returns the connected player, or NULL if the
connection
> > //process was aborted by the user.
> > PongClient::sptr waitForPlayer() {
> > LockCV lock(sync);
> >
> > while (!(player.lock()) && !kbhit()) {
> > //We wait for 250ms to recheck kbhit for pressed keys.
> > sync.timedWait(250);
> > }
> > if (!(player.lock())) {
> > //We were woken up by a keypress, so refuse any further
connections.
> > accept = false;
> >
> > //We don't need to wait around if anyone is in the middle of
connecting,
> > //because shutting down GNE will close any open connections, and
we will
> > //be closing down if we aborted this connection.
> >
> > //Return that no client connected.
> > return PongClient::sptr();
> > }
> >
> > return player.lock();
> > }

Unfortunately I don't have the time to properly review your code. I'll just
rewrite the above in a more "idiomatic" way (I hope I'm not missing
something.)

PongClient::sptr waitForPlayer()
{
    LockCV lock(sync);

    for(;;)
    {
        if( PongClient::sptr p = player.lock() )
        {
            return p;
        }
        else if( kbhit() )
        {
            accept = false;
            return PongClient::sptr();
        }
        else
        {
            sync.timedWait(250);
        }
    }
}

Even more "idiomatic" would be to use shared_ptr<...> instead of the ::sptr
typedef. Hiding the type of the smart pointer behind a typedef usually
doesn't help much and can lead to subtle errors since the semantics of the
various smart pointers differ in some corner cases.

HTH


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net