MSDN documentation says that instance methods, which includes Stream.Write, of Stream class are not guaranteed to be thread-safe but what does it mean? What will happen if one thread tries to call Stream.Write on a Stream object before other thread haven’t returned from the same method on same object? Will an exception be thrown, or the object will queue the data to send according to the order of threads? Some say it is ok to call without a locking mechanism. Can someone clarify this?
MSDN documentation says that instance methods, which includes Stream.Write, of Stream class are not
Share
It means that you should never call instance methods such as Read and Write on the same instance of Stream from different threads at the same time. You will get unexpected behavior. In some cases an exception might be thrown, in other your data might be corrupted and in other it might even work (if you are lucky enough).
You need to always synchronize access to such shared resources using proper locking mechanisms if you intend to use them from multiple threads.