Struct CyclicBarrier

Source
pub struct CyclicBarrier { /* private fields */ }
Expand description

Cyclic Barrier implementation.

It basically provides 2 methods:

let barrier = CyclicBarrier::new(count); // initialize a cyclic barrier with 3 parties barrier.wait(); // Wait until all parties are wait. Returnes Ok(()) for the last one entered the state

Most of the time, you should use Arc(barrier).

You can clone the Arc container of barrier as many times as you want. // You can reuse the barrier if the config does not need to be changed.

drop(barrier); // discard the barrier after you are done with it.

Usage: Example:

use std::sync::Arc;
use std::time::Duration;
use std::thread;
use classic_sync::cyclic_barrier::CyclicBarrier;
let barrier = CyclicBarrier::new(3);
let arc_barrier = Arc::new(barrier);
for i in 0..3 {
    let barrier_copy = Arc::clone(&arc_barrier);
    let tid = i;
    thread::spawn(move || {
        // if you care who's wait call triggered everyone to go, you can check the
        // firing object returned. If it is Some(_), it triggered the barrier
        let firing = barrier_copy.wait();
 
        if firing.is_some() {
            println!("Thread {tid} is the one triggered it!");
        }
        println!("Now we are starting almost at the same time!!");
    });
}
 
 

Implementations§

Source§

impl CyclicBarrier

Implementation of CyclicBarrier

Source

pub fn new(count: u32) -> CyclicBarrier

Create a new CyclicBarrier with count of count. It is backed by a C pthread_barrier_t object. count must be greater than 0. If not behavior is not determined.

Source

pub fn wait(&self) -> Option<()>

Wait for other parties If the caller is the last one enters the barrier and triggered the barrier release Then result is Some(()). Otherwise, result is None.

Trait Implementations§

Source§

impl Drop for CyclicBarrier

Close internal pthread_barrier_t object on Drop

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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.