Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 28, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/d2a6e63c5934659327beb47ccda189a4 to your computer and use it in GitHub Desktop.
Save rust-play/d2a6e63c5934659327beb47ccda189a4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(scoped_threads)]
use std::{thread, marker::PhantomData};
#[derive(Debug)]
struct SomeType(i32, PhantomData<*const()>); // make it !Send
// make it Sync. comment out to see compile error: cannot be shared between threads safely
unsafe impl Sync for SomeType {}
fn main() {
let mut T: SomeType = SomeType(0, PhantomData);
// &&mut T is Send, guranteed by cannot get `&mut T` from shared reference `&&T`
let r = &&mut T;
// uncomment below line to see compile error: &mut T is !Send because T is !Send
// let r = &mut T;
thread::scope(|s| {
s.spawn(|| {
let tmp = r;
println!("{:?}", tmp);
});
s.spawn(|| {
let tmp = r;
println!("{:?}", tmp);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment