Boost logo

Boost :

From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-09-01 12:35:32


On Mon, Sep 1, 2008 at 7:19 AM, Jaakko Järvi <jarvi_at_[hidden]> wrote:
> The review of Stjepan Rajko's Dataflow library starts today, September 1st,
> and will run until September 10th.
>

Hi, while skimming through the Dataflow documentation, I noticed an
error in the distributed example (http://tinyurl.com/5qcjxx):

void asio_server()
{
    ....
    {
        boost::mutex::scoped_lock lock(mutex_);
        acceptor.listen();
        cond.notify_all();
    }
....
}

int main(int, char* [])
{
    // start the server in a separate thread, and wait until it is listening
    boost::mutex::scoped_lock lock(mutex_);
    boost::thread t(asio_server);
    cond.wait(lock);
}
....

Cond is a condition variable, but it is being used wrongly. You need
to tie the waiting to a predicate (for example an external shared
flag, initially false) that must be checked in a loop:

int main(...) {
  ...
  boost::thread t(asio_server);
  {
     boost::mutex::scoped_lock lock(mutex_);
     while(!other_thread_ready)
         cond.wait(lock);
  }
  ...
}

and you should set it before the broadcast:

{
        boost::mutex::scoped_lock lock(mutex_);
        acceptor.listen();
        other_thread_ready = true;
        cond.notify_all();
}

BTW, hopefully I'll be able to write a review. The library seems interesting.

-- 
gpd

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk