Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Last active February 25, 2020 20:34
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 tos-kamiya/b0899d7d752b8cb3489551cc41fd07cb to your computer and use it in GitHub Desktop.
Save tos-kamiya/b0899d7d752b8cb3489551cc41fd07cb to your computer and use it in GitHub Desktop.
crossbeam scopre test
// https://docs.rs/crossbeam/0.7.3/crossbeam/thread/index.html#structs
// need to add the following dependency to Cargo.toml
//
// [dependencies]
// crossbeam = "0.7"
use crossbeam::thread;
fn main() {
let mut var = vec![1, 2, 3];
thread::scope(|s| {
s.spawn(|_| {
var.push(4);
println!("A child thread borrowing `var`: {:?}", var);
});
}).unwrap();
println!("Now got back ownership of `var`: {:?}", var);
}
// use std::thread;
//
// fn main() {
// let mut var = vec![1, 2, 3];
//
// thread::spawn(move || {
// var.push(4);
// println!("A child thread borrowing `var`: {:?}", var);
// });
//
// println!("Now got back ownership of `var`: {:?}", var); // compile error, ownership of `var` moved to the above thread.
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment