-
-
Save rust-play/d2a6e63c5934659327beb47ccda189a4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
#![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