Skip to main content

MpscFanIn

Struct MpscFanIn 

Source
pub struct MpscFanIn;
Expand description

Builder + handle factory for an N-producer fan-in. After construction, move each MpscFanInProducer to its producing thread and the single MpscFanInConsumer to its consuming thread.

Implementations§

Source§

impl MpscFanIn

Source

pub fn with_capacity<T: Send + 'static>( producer_count: usize, per_ring_capacity: usize, ) -> (Vec<MpscFanInProducer<T>>, MpscFanInConsumer<T>)

Build producer_count SPSC rings of capacity per_ring_capacity and return matched producer / consumer handles.

Examples found in repository?
examples/perf_features.rs (line 356)
355        fn fanin(cap: usize) -> (Vec<MpscFanInProducer<u64>>, MpscFanInConsumer<u64>) {
356            let (mut ps, c) = MpscFanIn::with_capacity::<u64>(PRODUCERS, cap);
357            for p in ps.iter_mut() {
358                for i in 0..cap / 2 {
359                    let _ = p.try_push(i as u64);
360                }
361            }
362            (ps, c)
363        }
More examples
Hide additional examples
examples/sample_app.rs (line 187)
181fn many_venue_fan_in() {
182    use subms_spsc_ring_buffer::MpscFanIn;
183
184    println!("\n== mpsc-fan-in: three venue feeds -> one strategy ==");
185    let venues = 3usize;
186    let per_venue = 20_000u64;
187    let (mut producers, mut consumer) = MpscFanIn::with_capacity::<Tick>(venues, 256);
188
189    let mut feeds = Vec::new();
190    for venue in 0..venues {
191        let mut p = producers.remove(0);
192        feeds.push(thread::spawn(move || {
193            for seq in 0..per_venue {
194                let tick = Tick {
195                    seq,
196                    price_cents: 40_000 + venue as u32,
197                };
198                while p.try_push(tick).is_err() {
199                    std::hint::spin_loop();
200                }
201            }
202        }));
203    }
204    let total = per_venue * venues as u64;
205    let strategy = thread::spawn(move || {
206        let mut got = 0u64;
207        while got < total {
208            if consumer.try_pop().is_some() {
209                got += 1;
210            }
211        }
212        got
213    });
214    for f in feeds {
215        f.join().unwrap();
216    }
217    let got = strategy.join().unwrap();
218    println!("  {venues} feeds x {per_venue} ticks -> consumer drained {got}");
219    assert_eq!(got, total);
220}

Auto Trait Implementations§

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.