并发读取和写入 NamePipeClientStream
我的应用程序中有一个 NamedPipeClientStream 实例,它设置为双工通信 (PipeDirection.InOut)。我还有两个线程,一个读取线程和一个写入线程。
我想让读取线程仅调用 NamedPipeClientStream.Read 方法,而写入线程仅调用 NamedPipeClientStream.Write 方法。他们永远不会调用彼此的方法,但他们可能会同时调用管道实例。
我查找了 NamedPipeClientStream 的文档,它说公共静态方法是线程安全的,但实例方法不能保证线程安全。
我的问题是,我有两个线程同时在管道实例上调用两个不同的方法(读取和写入)是否安全,或者这是我不应该做的事情?而且,实例方法线程安全仅适用于调用同一方法的单独线程,而不适用于调用单独方法(如 Read 和 Write)的单独线程吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,没问题。 PipeStream.Read和Write都直接调用本机Windows API,ReadFile和WriteFile是线程安全的。
Yes, no problem. Both PipeStream.Read and Write directly call the native Windows API, ReadFile and WriteFile are thread safe.