Why does writing to stdout/stderr from multiple threads results in interleaved output? #321
-
What version of the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
This is absolutely expected behavior. Your Basically, you're creating a bunch of threads, each with their own CSV writer, but on top of the same underlying I can think of two very simple modifications to your code:
|
Beta Was this translation helpful? Give feedback.
This is absolutely expected behavior. Your
println!code is not equivalent. When you callprintln!, it's internally callingstd::io::stdout().lock(). Thecsv::Writerdoes no such thing. All it takes is something that implementsstd::io::Write. There's nolock()method to call in that context. Instead, it's up to you, the caller, to either pass a locked writer or manage the synchronization yourself.Basically, you're creating a bunch of threads, each with their own CSV writer, but on top of the same underlying
std::io::Writeimplementation. The CSV writer might callwritemultiple times on the given writer for eachserializecall, and thus the output is interleaved. And it is expected and …