TrAdder

Struct TrAdder 

Source
pub struct TrAdder<T: Copy + Zero + Add<T, Output = T> + Send>(/* private fields */);
Expand description

The threaded add allows to increment and decrement an integer from multiple threads without contention, which allows performance to scale well with the number of thread/processors. TrAdder can wrap any primitive integer type.

Overflow behavior. Overflow may occur if the sum of the increments in any subset of the threads overflows, even if the total leads to no overflow. Overflow semantic is the same as for primitive types (panic or wrapping).

See TrAcc for a discussion of performance characteristics.

use hytra::TrAdder;
let adder: TrAdder<i64> = TrAdder::new();
crossbeam_utils::thread::scope(|s| {
    for _ in 0..10 {
        s.spawn(|_| {
            for _ in 0..10 {
                adder.inc(1);
            }
        });
    }
})
.unwrap();
assert_eq!(adder.get(), 100);

Implementations§

Source§

impl<T: Copy + Zero + Add<T, Output = T> + Send> TrAdder<T>

Source

pub fn new() -> Self

Create a new TrAdder initialized to 0.

Source

pub fn inc(&self, x: T)

Increment the TrAdder.

Source

pub fn get(&self) -> T

Return the value of the TrAdder.

Trait Implementations§

Source§

impl<T: Debug + Copy + Zero + Add<T, Output = T> + Send> Debug for TrAdder<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + Zero + Add<T, Output = T> + Send> Default for TrAdder<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for TrAdder<T>

§

impl<T> RefUnwindSafe for TrAdder<T>
where T: RefUnwindSafe,

§

impl<T> Send for TrAdder<T>

§

impl<T> Sync for TrAdder<T>
where T: Sync,

§

impl<T> Unpin for TrAdder<T>
where T: Unpin,

§

impl<T> UnwindSafe for TrAdder<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.