Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::thread;
- #[derive(Copy, Clone)]
- struct UnsafeIntPtr(*mut i32);
- unsafe impl Send for UnsafeIntPtr {}
- fn main() {
- let mut n = 0;
- let n = UnsafeIntPtr(&mut n);
- let a = thread::spawn(move || add_500_000(n.0));
- let b = thread::spawn(move || add_500_000(n.0));
- a.join().unwrap();
- b.join().unwrap();
- println!("{}", unsafe { *n.0 } );
- }
- fn add_500_000(n: *mut i32) {
- for _ in 0..500_000 {
- unsafe { *n += 1; }
- }
- }
Add Comment
Please, Sign In to add comment