Skip to main content

Module shared_box

Module shared_box 

Source
Expand description

§Manually shared box

use std::thread;
use manual_share::SharedBox;

let b = Box::new(13);
let mut b = SharedBox::from_box(b);

let br = b.borrow();
let j1 = thread::spawn(move || {
    println!("{}", br.get());
    br
});

let br = b.borrow();
let j2 = thread::spawn(move || {
    println!("{}", br.get() + 1);
    br
});

b.try_return(j1.join().unwrap()).unwrap();
b.try_return(j2.join().unwrap()).unwrap();

let b = b.try_into_box().unwrap();
println!("{:?}", b);

Structs§

SharedBox
A structure owning the original Box which can be used to create multiple SharedBoxRef to send to other thread. It uses a counter to record the number of SharedBoxRef that has been created and not given back.
SharedBoxRef
A Reference to SharedBox that can be sent to other threads.