unresolved_external
unresolved_external

Reputation: 2018

How to ensure all pipe buffer has been read?

I have an app on Windows which has two threads:

First one - is reading information from some external storage and writing it to a pipe via WriteFile

Second one - is reading information from the pipe using ReadFileand doing some manipulation with it.

I am using usual anonymous pipe created via CreatePipe. So my question is - when I close the pipe in the writing thread sometimes the reading thread has not finished reading, which leads to a broken pipe. So how can I ensure, that all written information has been read before closing the pipe?

Upvotes: 0

Views: 219

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597051

You could try FlushFileBuffers() before closing the pipe.

Another option would be to create two event objects with CreateEvent(). When the writing thread is done writing, signal the first event with SetEvent() and wait for the second event to be signaled using WaitForSingleObject() before then closing the pipe (or writing the next data block). When the reading thread detects the first event has been signaled, read from the pipe until there is nothing left to read and then signal the second event using SetEvent().

Upvotes: 1

Related Questions