A thread-safe weighted reference counting smart-pointer for Rust.
By using weights instead of direct reference counting WRC requires roughly half as many synchronisation operations and writes to the heap. Every time a WRC is cloned it's weight is split in two, with half allocated to the parent and half allocated to the child. When a WRC is dropped it's weight is removed from the total. When the total weight declines to zero then the referenced object is dropped.
Sharing some immutable data between threads:
```rust use wrc::WRC; use std::thread;
let five = WRC::new(5);
for _ in 0..10 { let five = five.clone(); thread::spawn(move || { println!("{:?}", five); }); } ```
Sharing a mutable AtomicUsize
:
```rust use wrc::WRC; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread;
let val = WRC::new(AtomicUsize::new(5));
for _ in 0..10 { let val = val.clone();
thread::spawn(move || {
let v = val.fetch_add(1, Ordering::SeqCst);
println!("{:?}", v);
});
} ```
This create relies on the (currently) unstable shared
, unsize
and coerce_unsized
features, meaning that you need to run an unstable Rust. Other than that it seems legit.
Source code is licensed under the terms of the MIT license, the text of which is included in the LICENSE file in this distribution.