Created
April 24, 2025 21:54
-
-
Save rectalogic/881f797f987e99a0c9f1dacb2530e0dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::mpsc; | |
use std::{thread, time}; | |
struct Guard { | |
tx: mpsc::SyncSender<bool>, | |
} | |
impl Drop for Guard { | |
fn drop(&mut self) { | |
println!("in drop about to send"); | |
self.tx.send(true); | |
println!("in drop send finished"); | |
} | |
} | |
fn main() { | |
let (tx, rx) = mpsc::sync_channel::<bool>(0); | |
let guard = Guard { tx }; | |
thread::spawn(move || { | |
println!("thread sleeping"); | |
thread::sleep(time::Duration::from_secs(5)); | |
println!("thread sleep finished, dropping guard"); | |
drop(guard); | |
}); | |
println!("main thread about to wait"); | |
rx.recv(); | |
println!("main thread finished"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment