views:

286

answers:

3

Why does Console.WriteLine work from multiple threads?

+1  A: 

Multiple threads write to the same output when using Console.WriteLine, generally your screen by default.

Chris Ballance
So Console.WriteLine is coded as thread safe then? One thread blocks while the other writes?
It is - see my answer for the details from MSDN.
Reed Copsey
+3  A: 

The console class handles the thread syncrhonization for you.

From the documentation of Console:

I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams.
Reed Copsey
Yes, thank you Reed.
+1 Thanks for the clarification upon my answer, well put.
Chris Ballance
A: 

Reed, I couldn't figure out how to add a comment, hence the answer tag. Of course, you probably won't see this anyhow, it's been a long time since the original post.

I'm wondering if you are quoting MSDN out of context. The full statement is:

"By default, the value of the In property is a System.IO.TextReader object, and the values of the Out and Error properties are System.IO.TextWriter objects. However, you can set these properties to streams that do not represent the console; for example, you can set these properties to streams that represent files. To redirect the standard input, standard output, or standard error stream, call the SetIn, SetOut, or SetError method, respectively. I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams."

I'm wondering if "these streams" refers just to file streams or if it refers to all streams including TexReader and TextWriter. Thoughts?

Dave