Synchronization of writing to stdout in threads


#1

I have different threads where I print out stuff to stdout using println!().

Question: Does println! make sure things are synchronized or do I have to use something like

     let stdout = io::stdout(); 
     let mut stdout = stdout.lock();
     let _ = writeln!(&mut stdout, "blabla);

?


#2

By default, stdout is locked at the granularity of individual println!() statements. You only need stdout.lock() when you want a coarser locking granularity.


#3

If I understand you correctly this means that I can be sure that the output of two println!() statements from different threads won’t get mixed up (hope my English is ok here). This is what I want to have.


#4

Yes. Two println!() from different threads may be printed in any order, but you won’t get a mixture of both.