Skip to content

Commit 0ec75ab

Browse files
committed
Lock stdout for the whole duration of print
write_fmt calls write for each formatted field. The default implementation of write_fmt is used, which will call write on not-yet-locked stdout (and write locking after), therefore making print! in multithreaded environment still interleave contents of two separate prints.
1 parent 80def6c commit 0ec75ab

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/libstd/io/stdio.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,9 @@ pub fn _print(args: fmt::Arguments) {
398398
return w.write_fmt(args);
399399
}
400400
}
401-
stdout().write_fmt(args)
401+
let stdout = stdout();
402+
let mut lock = stdout.lock();
403+
lock.write_fmt(args)
402404
});
403405
if let Err(e) = result {
404406
panic!("failed printing to stdout: {}", e);

0 commit comments

Comments
 (0)