I have a pipe handle that was created as overlapped. I need to read and write data to it in parallel. How can I achieve this?
Named Pipe Server Using Overlapped I/O documentation page shows an example how to read and write to many pipes, but "it avoids simultaneous operations on a single pipe instance".
What is the right way to do this in C++ on Windows? I can't file the right example nor some help on the topic.
The main problem I face that normal ReadFile blocks when there is no data to read and eventually I can't write with WriteFile. I haven't found any method that can tell me is there something to read that don't block. As far as I understand I need to pass OVERLAPPED structure but don't know how to use it in case of parallel read and write to one pipe (not many).
It should be possible as it is said in Synchronous and Overlapped Pipe I/O:
Overlapped operations make it possible for one pipe to read and write data simultaneously and for a single thread to perform simultaneous I/O operations on multiple pipe handles.
All you need to do is to provide a different OVERLAPPED structure to each of the simultaneous operations. In your case, all that means is that each of the two threads needs its own OVERLAPPED structure. Since the threads are presumably running different functions, this should happen automatically unless you mess it up by using a global variable.
Note that you're over-complicating things by starting from that sample, which is focused on using overlapped I/O to avoid the need for multiple threads.
Instead, pretend you're writing each of the two functions using non-overlapped I/O, but whenever you would call ReadFile or WriteFile, include a valid OVERLAPPED structure with an event handle and follow up with WaitForSingleObject. There are a few things you should know: you have to make sure that the threads each create their own event object, and you have to handle the case where the I/O operation completes immediately (i.e., returns ERROR_SUCCESS instead of ERROR_IO_PENDING). Otherwise it's all fairly straightforward.
If you can't make it work, show your code.