scope-lock-0.2.1 has been yanked.
Scope lock
Examples
Using references
use std::thread;
let mut a = vec![1, 2, 3];
let mut x = 0;
let f1 = &|()| {
println!("hello from the first scoped thread");
dbg!(&a);
};
let f2 = &mut |()| {
println!("hello from the second scoped thread");
x += a[0] + a[2];
};
scope_lock::lock_scope(|e| {
thread::spawn({
let f = e.extend_fn(f1);
move || f.call(())
});
thread::spawn({
let mut f = e.extend_fn_mut(f2);
move || f.call(())
});
println!("hello from the main thread");
});
a.push(4);
assert_eq!(x, a.len());
Using boxes
use std::thread;
let mut a = vec![1, 2, 3];
let mut x = 0;
scope_lock::lock_scope(|e| {
thread::spawn({
let f = e.extend_fn_once_box(|()| {
println!("hello from the first scoped thread");
dbg!(&a);
});
move || f(())
});
thread::spawn({
let mut f = e.extend_fn_once_box(|()| {
println!("hello from the second scoped thread");
x += a[0] + a[2];
});
move || f(())
});
println!("hello from the main thread");
});
a.push(4);
assert_eq!(x, a.len());