Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Created April 24, 2025 21:54
Show Gist options
  • Save rectalogic/881f797f987e99a0c9f1dacb2530e0dc to your computer and use it in GitHub Desktop.
Save rectalogic/881f797f987e99a0c9f1dacb2530e0dc to your computer and use it in GitHub Desktop.
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