pub struct WaitGroup { /* private fields */ }
Expand description
A waitgroup waits for a collection of tasks to complete. It keeps track of tasks via shared counter
Implementations§
Source§impl WaitGroup
impl WaitGroup
Sourcepub fn new(n: usize) -> WaitGroup
pub fn new(n: usize) -> WaitGroup
creates a new wait group instance
Examples found in repository?
examples/threads.rs (line 8)
7fn main() {
8 let wg = Arc::new(WaitGroup::new(0));
9 for _ in 0..100 {
10 wg.add(1);
11 let wg2 = wg.clone();
12 thread::spawn(move || {
13 thread::sleep_ms(2000);
14 wg2.done();
15 });
16 }
17 wg.wait();
18 println!("done")
19}
Sourcepub fn add(&self, delta: usize)
pub fn add(&self, delta: usize)
adds delta
to internal counter
Examples found in repository?
examples/threads.rs (line 10)
7fn main() {
8 let wg = Arc::new(WaitGroup::new(0));
9 for _ in 0..100 {
10 wg.add(1);
11 let wg2 = wg.clone();
12 thread::spawn(move || {
13 thread::sleep_ms(2000);
14 wg2.done();
15 });
16 }
17 wg.wait();
18 println!("done")
19}
Sourcepub fn done(&self)
pub fn done(&self)
subtracts 1 from internal counter
Examples found in repository?
examples/threads.rs (line 14)
7fn main() {
8 let wg = Arc::new(WaitGroup::new(0));
9 for _ in 0..100 {
10 wg.add(1);
11 let wg2 = wg.clone();
12 thread::spawn(move || {
13 thread::sleep_ms(2000);
14 wg2.done();
15 });
16 }
17 wg.wait();
18 println!("done")
19}
Sourcepub fn wait(&self)
pub fn wait(&self)
blocks the current thread until wait group is complete
Examples found in repository?
examples/threads.rs (line 17)
7fn main() {
8 let wg = Arc::new(WaitGroup::new(0));
9 for _ in 0..100 {
10 wg.add(1);
11 let wg2 = wg.clone();
12 thread::spawn(move || {
13 thread::sleep_ms(2000);
14 wg2.done();
15 });
16 }
17 wg.wait();
18 println!("done")
19}
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for WaitGroup
impl RefUnwindSafe for WaitGroup
impl Send for WaitGroup
impl Sync for WaitGroup
impl Unpin for WaitGroup
impl UnwindSafe for WaitGroup
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more