Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Synchronous/Asynchronous pattern
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2009-03-10 06:26:40


Igor R wrote:

>> What is the best way to "emulate" synchronous behaviour with asio? The
>> thread running this iteration should not block, but do other work (at the
>> io_service?) until the requested results are received.
>
> Probably, I misunderstood you, but aren't these 2 requirements
> contradicting? If you wish synchronous processing of messages, then
> each iteration in your loop should block and wait until the i/o is
> done.

I would like to "fake" the completion handler. I have managed to get
something like it working, see below.

void handle_request() {
  result_available = false;
  async_write( ... request );
  // .. and more async-stuf
  // do other stuff, fake a blocking wait
  while ( !result_available ) {
    m_io_sevice.run_one();
  }
  return result;
}

void handle_read() {
  ...
  result_available = true;
}

bool result_available;

However, I'm not sure whether to choose of poll_one or run_one in this case.
I.e., I'm looking for the cheapest "do some other stuff" of asio's
io_service.

Cheers,

Rutger


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