Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Last active August 16, 2020 09:41
Show Gist options
  • Save justanotherdot/9a8cf84be4ba17326903f4eb5f4974d6 to your computer and use it in GitHub Desktop.
Save justanotherdot/9a8cf84be4ba17326903f4eb5f4974d6 to your computer and use it in GitHub Desktop.
fn main() {
let x = String::from("foo");
// this does not work because it uses the special `'static` lifetime.
// which means _the entire program_. The main thread could finish and this
// thread could still be running! In that case the reference to x would be invalid.
// we can make this work by passing `move` in front of the closure,
// effectively copying `x` into the closure's environment.
// let normal = std::thread::spawn(|| dbg!(&x));
// normal.join();
// this works because it adopts the lifetime of `main`.
let scoped = crossbeam::thread::scope(|s| {
let child = s.spawn(|_| dbg!(&x));
child.join()
});
dbg!(&scoped);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment