Expand description
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§
- Shared
Bool - Shareable boolean data element.
- Shared
F32 - Stores a f32 element inside an atomic element so it can be accessed by multiple threads.
- Shared
F64 - Shareable f64 data element.
- Shared
I8 - Shareable i8 data element.
- Shared
I16 - Shareable i16 data element.
- Shared
I32 - Shareable i32 data element.
- Shared
I64 - Shareable i64 data element.
- Shared
Isize - Shareable isize data element.
- Shared
Object - Shareable object data element.
- Shared
U8 - Shareable u8 data element.
- Shared
U16 - Shareable u16 data element.
- Shared
U32 - Shareable u32 data element.
- Shared
U64 - Shareable u64 data element.
- Shared
Usize - Shareable usize data element.