Skip to content

Instantly share code, notes, and snippets.

@utaal
Created April 14, 2020 14:04
Show Gist options
  • Save utaal/8dc2bf0d1aa61a03f665e7b76f27a299 to your computer and use it in GitHub Desktop.
Save utaal/8dc2bf0d1aa61a03f665e7b76f27a299 to your computer and use it in GitHub Desktop.
spsc-bip-buffer potential data race
fn grant_data_race() {
for i in 0..128 {
let (mut writer, mut reader) = bip_buffer_from(vec![0u8; 16].into_boxed_slice());
let mut reservation = writer.reserve(8);
let sender = std::thread::spawn(move || {
writer.reserve(8).as_mut().expect("reserve").copy_from_slice(&[10, 11, 12, 13, 14, 15, 16, i]);
});
reservation.as_mut().expect("reserve").copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]);
let receiver = std::thread::spawn(move || {
while reader.valid().len() < 8 {}
reader.consume(8);
});
sender.join().unwrap();
receiver.join().unwrap();
}
}
error[E0505]: cannot move out of `writer` because it is borrowed
--> src/lib.rs:682:45
|
681 | let mut reservation = writer.reserve(8);
| ------ borrow of `writer` occurs here
682 | let sender = std::thread::spawn(move || {
| ^^^^^^^ move out of `writer` occurs here
683 | writer.reserve(8).as_mut().expect("reserve").copy_from_slice(&[10, 11, 12, 13, 14, 15, 16, i]);
| ------ move occurs due to use in closure
684 | });
685 | reservation.as_mut().expect("reserve").copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]);
| ----------- borrow later used here
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment