#thread #atomic #data #share #64-bit #reference-counting

shareable

Thread shareable objects using the minimal amount of synchronization

4 releases

Uses old Rust 2015

0.1.1 Jun 12, 2016
0.1.0 May 12, 2016
0.0.5 May 11, 2016
0.0.1 May 9, 2016

#856 in Concurrency


Used in valid_toml

AML/Apache-2.0

68KB
1.5K SLoC

shareable

This crate allows the sharing of various data objects via atomic memory operations to avoid the overhead of Mutex whenever possible. The SharedF32, SharedI8, SharedI16, SharedI32, SharedIsize, SharedU8, SharedU16, SharedU32, and SharedUsize objects basically just pass the data to the atomic elements.

The SharedF64, SharedI64 and SharedU64 objects are useful when supporting both 32 and 64 bit environments. Under 64 bit environments they will use the faster atomic data objects to share the values, when compiled under 32 bit they will use the slower mutexes since the atomics only handle 32 bit values.

The SharedObject will share a read only object. Internally it uses a very small spin lock to to insure accurate reference counting but should provide faster access than using a Mutex.

Examples

use std::sync::mpsc;
use std::thread;
use shareable::SharedObject;

let value1 = SharedObject::new(String::from("abc"));
let value2 = value1.clone();

let (tx, rx) = mpsc::channel();

let thread = thread::spawn(move || {
    rx.recv();
    assert_eq!(value2.get().as_str(), "xyz");
});

value1.set(String::from("xyz"));

tx.send(());
thread.join().unwrap();

No runtime deps