1

I'm looking for a way for release std::io::StdinLock externally.

In the following case, two threads are spawned (not counting the main thread) named read_stdin and ttl.

  • read_stdin is locked by handle.read_line and waits for read_line to free itself.
  • ttl waits 2 seconds to free itself.

Is there a way to release read_stdin when ttl is released?

use std::io;
use std::io::BufRead;
use std::thread;
use std::time::Duration;

fn main() {
    let read_stdin = thread::spawn(move || {
        let mut buffer = String::new();
        let stdin = io::stdin();
        let mut handle = stdin.lock();
        handle.read_line(&mut buffer).unwrap();
        println!("{}", buffer);
    });

    let ttl = thread::spawn(move || {
        thread::sleep(Duration::from_secs(2));
        println!("end");
    });

    ttl.join();
    read_stdin.join();
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Vincent
  • 391
  • 3
  • 10
  • The lock is released when the lock guard goes out of scope. See [How to “unlock” an RwLock?](https://stackoverflow.com/q/47916718/155423) – Shepmaster Jun 03 '20 at 12:31
  • 1
    It looks like your question might be answered by the answers of [What is the standard way to get a Rust thread out of blocking operations?](https://stackoverflow.com/q/52465480/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 03 '20 at 12:32
  • you don't that the very specific point of having lock – Stargateur Jun 03 '20 at 13:20
  • Thanks @Stargateur for your answer, it resolve my issue. – Vincent Jun 03 '20 at 14:00

0 Answers0