Synchronization of writing to stdout in threads

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);

?

1 Like

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

2 Likes

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.

1 Like

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

2 Likes