Skip to content

Instantly share code, notes, and snippets.

@ms747
Created November 22, 2022 08:57
Show Gist options
  • Save ms747/31b6c49a5c66b367b1de69a4baf12d71 to your computer and use it in GitHub Desktop.
Save ms747/31b6c49a5c66b367b1de69a4baf12d71 to your computer and use it in GitHub Desktop.
Access array between threads without Arc/Mutex
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