Home > Software design >  Multiple instances of StreamWriter working on multiple different disk files in multiple different ta
Multiple instances of StreamWriter working on multiple different disk files in multiple different ta

Time:05-01

I have a C# application that creates multiple tasks (class System.Threading.Tasks.Task). Each of these tasks writes to a file, each one writes to a different file from the others, using a different StreamWriter instance.

Microsoft's docs says that: "By default, a StreamWriter is not thread safe."

This means that I could run into problems also in the case described above?

CodePudding user response:

Since you are saying each task is writing to a different file with its own dedicated instance of StreamWriter, you do not have a problem. You need not do synchronization in your scenario. Only if a single streamwriter instance is used by multiple thread functions then there is a need to do lock based synchronization.

CodePudding user response:

"StreamWriter is not thread safe." only means that it is not safe to use multiple threads on the SAME StreamWriter instance.

  • Related