Skip to content

Is StdoutLock guranteed to be reentrant? #134698

Open
@FeldrinH

Description

Location

https://doc.rust-lang.org/std/io/struct.Stdout.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stderr.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stdin.html#method.lock

Summary

Currently code like this is fine and a convenient way to lock stdout to prevent other threads from interrupting while printing is happening (this is especially useful when the printing happens deep inside some library/component, where StdoutLock can't be easily passed as an argument):

let stdout = io::stdout().lock();
println!("a");
println!("b");
println!("c");
drop(stdout);

As far as I understand, this works because Stdout::lock() is reentrant. However, the documentation does not mention that Stdout::lock() is reentrant.

Is Stdout::lock() guaranteed to be reentrant in all future Rust versions, or is this an implementation detail that should not be relied on?
Either way it would be nice if the documentation of Stdout::lock() made it clear what the expected behavior is and how much of it is an implementation detail.

The same question also applies to Stderr::lock() and Stdin::lock().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions