Use scoped threads in std::sync::Barrier
examples
This removes boilerplate around `Arc`s and makes the code more clear.
This commit is contained in:
parent
32c8a9f49d
commit
6a89f8789a
1 changed files with 26 additions and 34 deletions
|
@ -10,26 +10,22 @@ use crate::sync::{Condvar, Mutex};
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::sync::{Arc, Barrier};
|
/// use std::sync::Barrier;
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// let n = 10;
|
/// let n = 10;
|
||||||
/// let mut handles = Vec::with_capacity(n);
|
/// let barrier = Barrier::new(n);
|
||||||
/// let barrier = Arc::new(Barrier::new(n));
|
/// thread::scope(|s| {
|
||||||
/// for _ in 0..n {
|
/// for _ in 0..n {
|
||||||
/// let c = Arc::clone(&barrier);
|
/// // The same messages will be printed together.
|
||||||
/// // The same messages will be printed together.
|
/// // You will NOT see any interleaving.
|
||||||
/// // You will NOT see any interleaving.
|
/// s.spawn(|| {
|
||||||
/// handles.push(thread::spawn(move || {
|
/// println!("before wait");
|
||||||
/// println!("before wait");
|
/// barrier.wait();
|
||||||
/// c.wait();
|
/// println!("after wait");
|
||||||
/// println!("after wait");
|
/// });
|
||||||
/// }));
|
/// }
|
||||||
/// }
|
/// });
|
||||||
/// // Wait for other threads to finish.
|
|
||||||
/// for handle in handles {
|
|
||||||
/// handle.join().unwrap();
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Barrier {
|
pub struct Barrier {
|
||||||
|
@ -105,26 +101,22 @@ impl Barrier {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::sync::{Arc, Barrier};
|
/// use std::sync::Barrier;
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// let n = 10;
|
/// let n = 10;
|
||||||
/// let mut handles = Vec::with_capacity(n);
|
/// let barrier = Barrier::new(n);
|
||||||
/// let barrier = Arc::new(Barrier::new(n));
|
/// thread::scope(|s| {
|
||||||
/// for _ in 0..n {
|
/// for _ in 0..n {
|
||||||
/// let c = Arc::clone(&barrier);
|
/// // The same messages will be printed together.
|
||||||
/// // The same messages will be printed together.
|
/// // You will NOT see any interleaving.
|
||||||
/// // You will NOT see any interleaving.
|
/// s.spawn(|| {
|
||||||
/// handles.push(thread::spawn(move || {
|
/// println!("before wait");
|
||||||
/// println!("before wait");
|
/// barrier.wait();
|
||||||
/// c.wait();
|
/// println!("after wait");
|
||||||
/// println!("after wait");
|
/// });
|
||||||
/// }));
|
/// }
|
||||||
/// }
|
/// });
|
||||||
/// // Wait for other threads to finish.
|
|
||||||
/// for handle in handles {
|
|
||||||
/// handle.join().unwrap();
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn wait(&self) -> BarrierWaitResult {
|
pub fn wait(&self) -> BarrierWaitResult {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue