开发者

is it safe to read the pipe when the other side is writing to it?

开发者 https://www.devze.com 2023-01-12 22:45 出处:网络
fSuccess = ReadFile( hPipe,// pipe handle chBuf,// buffer to receive reply BUFSIZE*sizeof(TCHAR),// size of buffer
      fSuccess = ReadFile( 
         hPipe,    // pipe handle 
         chBuf,    // buffer to receive reply 
         BUFSIZE*sizeof(TCHAR),  // size of buffer 
         &cbRead,  // number of bytes read 
         NULL);    // not overlapped 

If not safe, how can I ensure the other side is not writin开发者_JAVA技巧g when reading a pipe in windows?


Yes, this is a perfectly legal operation on the pipe. One end of the pipe can read from and write to a pipe irrespective of what is happening to the other end.


It's perfectly safe -- pipes handle all the necessary synchronization on the buffers and such automatically. If you try to write to/read from a pipe when the other process has closed its connection to the pipe (either explicitly by closing the pipe, or implicitly by exiting the process) you'll get ERROR_BROKEN_PIPE from GetLastError. If you're using anonymous pipes the parent process will typically look for this to detect when the child process exited, so there will be no more data to process.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号