]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/threadleak_ignored.rs
Auto merge of #104915 - weihanglo:update-cargo, r=ehuss
[rust.git] / src / tools / miri / tests / pass / threadleak_ignored.rs
1 //@compile-flags: -Zmiri-ignore-leaks
2
3 //! Test that leaking threads works, and that their destructors are not executed.
4
5 use std::cell::RefCell;
6
7 struct LoudDrop(i32);
8 impl Drop for LoudDrop {
9     fn drop(&mut self) {
10         eprintln!("Dropping {}", self.0);
11     }
12 }
13
14 thread_local! {
15     static X: RefCell<Option<LoudDrop>> = RefCell::new(None);
16 }
17
18 fn main() {
19     X.with(|x| *x.borrow_mut() = Some(LoudDrop(0)));
20
21     // Set up a channel so that we can learn when the other thread initialized `X`
22     // (so that we are sure there is something to drop).
23     let (send, recv) = std::sync::mpsc::channel::<()>();
24
25     let _detached = std::thread::spawn(move || {
26         X.with(|x| *x.borrow_mut() = Some(LoudDrop(1)));
27         send.send(()).unwrap();
28         std::thread::yield_now();
29         loop {}
30     });
31
32     std::thread::yield_now();
33
34     // Wait until child thread has initialized its `X`.
35     let () = recv.recv().unwrap();
36 }