Created
November 22, 2022 08:57
-
-
Save ms747/31b6c49a5c66b367b1de69a4baf12d71 to your computer and use it in GitHub Desktop.
Access array between threads without Arc/Mutex
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::thread::spawn; | |
fn main() { | |
let mut threads = vec![]; | |
let my_array: &'static [u8; 16] = Box::leak(Box::new([0; 16])); | |
for i in 0..4 { | |
let thread = spawn(move || { | |
let my_array = my_array.as_ptr() as *mut u8; | |
unsafe { | |
my_array.offset(i).write(69); | |
} | |
}); | |
threads.push(thread); | |
} | |
for thread in threads { | |
thread.join().unwrap(); | |
} | |
println!("{:?}", my_array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment