Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio blocking socket read with timeout with multiple threads
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2018-03-22 22:31:09


On 22/03/2018 22:56, Thomas Quarendon wrote:
> Yes, sorry. Yes, you can create pipes in Windows, but asio doesn't
support Windows anonymous pipes or concole handles, as they don't
support completion ports. But yes, you can create a named port with a
unique name (which is what CreatePipe does).

Anonymous pipes created with CreatePipe don't support overlapped I/O,
true; but as you said yourself they're just named pipes with a unique
name, and CreateNamedPipe *can* support overlapped I/O, so you can make
up your own name and use them with a windows::stream_handle to do async
reads and writes.

It's a little more fiddly but you can also compose your own I/O objects
and operations around other APIs, presumably such as TransactNamedPipe.

I have some code that turns a shared memory block (with events) into a
simple one-slot interprocess ASIO-compatible async mailbox, for example;
the key trick was to use windows::object_handle::async_wait to "wait"
for something to happen and then read the data synchronously in the wait
completion handler (because at that point it is guaranteed to complete
without blocking).

> Using async initially, then sync to read the "message" works well,
it's a nice design, if only I could put a timeout on the synchronous reads.

You can use a deadline_timer and cancel the synchronous read if it
trips. It's a bit fugly though and is subject to races. It's much
nicer to use async.


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