
Rutger ter Borg wrote:
In an application we would like to support synchronous operations in the application layer, whereas operations below it are done with asynchronous operations. Callback handlers are not appropriate in this case,
To make the example more clear, suppose you have a remote_vector which you would like to iterate through,
remote_vector a(10); for( int i=0; i<10; ++i ) { std::cout << a[i] << std::endl; }
here, a[i] will cause messages to be sent to retrieve the actual vector data before printing it. The network layer is hidden from the user.
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.
I don't really understand the requirement but I think it boils down to whether the other work you want to do is coupled to the async network i/o or not. If it isn't then surely that can just be done in it's own thread and the network i/o can simply be synchronous. If it is then I would use a pair of condition variables to pass commands and results between a "synchronous" layer to an "asynchronous" layer which are just 2 separate threads. If there is no pending asynchronous event to keep the io service alive then there are documented ways of dealing with this using the io_service::work object.
Many thanks, Kind regards,
Rutger ter Borg
HTH -- Bill Somerville