Crate shareable [] [src]

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();

Structs

SharedBool

Shareable boolean data element.

SharedF32

Stores a f32 element inside an atomic element so it can be accessed by multiple threads.

SharedF64

Shareable f64 data element.

SharedI16

Shareable i16 data element.

SharedI32

Shareable i32 data element.

SharedI64

Shareable i64 data element.

SharedI8

Shareable i8 data element.

SharedIsize

Shareable isize data element.

SharedObject

Shareable object data element.

SharedU16

Shareable u16 data element.

SharedU32

Shareable u32 data element.

SharedU64

Shareable u64 data element.

SharedU8

Shareable u8 data element.

SharedUsize

Shareable usize data element.