Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent reading and writing to NamePipeClientStream

I have a NamedPipeClientStream instance in my application that is setup for duplex communication (PipeDirection.InOut). I also have two threads, a read thread and a write thread.

I want to have the read thread call only the NamedPipeClientStream.Read method, and the write thread only call the NamedPipeClientStream.Write method. They will never be calling each others methods, but they may be making calls to the pipe instance at the same time.

I looked up the documentation for the NamedPipeClientStream and it said that public static methods are thread safe, but instance methods are not guaranteed to be thread safe.

My question is is it safe that I have two threads calling two different methods (Read and Write) on the pipe instance at the same time, or is this something I should not do? And, does the instance method thread safety only apply to separate threads calling the same method and not separate threads calling separate methods like Read and Write?

Thanks!

like image 978
Chris Avatar asked Nov 01 '25 11:11

Chris


1 Answers

Yes, no problem. Both PipeStream.Read and Write directly call the native Windows API, ReadFile and WriteFile are thread safe.

like image 140
Hans Passant Avatar answered Nov 04 '25 02:11

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!