Skip to content

Commit 8f4f474

Browse files
authored
Rollup merge of rust-lang#40010 - GuillaumeGomez:barrier-docs, r=frewsxcv
Add missing urls and examples into Barrier structs r? @frewsxcv
2 parents 9eebcf1 + 58a9dd3 commit 8f4f474

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

src/libstd/sync/barrier.rs

+69-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use sync::{Mutex, Condvar};
1414
/// A barrier enables multiple threads to synchronize the beginning
1515
/// of some computation.
1616
///
17+
/// # Examples
18+
///
1719
/// ```
1820
/// use std::sync::{Arc, Barrier};
1921
/// use std::thread;
@@ -50,8 +52,19 @@ struct BarrierState {
5052

5153
/// A result returned from wait.
5254
///
53-
/// Currently this opaque structure only has one method, `.is_leader()`. Only
55+
/// Currently this opaque structure only has one method, [`.is_leader()`]. Only
5456
/// one thread will receive a result that will return `true` from this function.
57+
///
58+
/// [`.is_leader()`]: #method.is_leader
59+
///
60+
/// # Examples
61+
///
62+
/// ```
63+
/// use std::sync::Barrier;
64+
///
65+
/// let barrier = Barrier::new(1);
66+
/// let barrier_wait_result = barrier.wait();
67+
/// ```
5568
#[stable(feature = "rust1", since = "1.0.0")]
5669
pub struct BarrierWaitResult(bool);
5770

@@ -65,8 +78,18 @@ impl fmt::Debug for Barrier {
6578
impl Barrier {
6679
/// Creates a new barrier that can block a given number of threads.
6780
///
68-
/// A barrier will block `n`-1 threads which call `wait` and then wake up
69-
/// all threads at once when the `n`th thread calls `wait`.
81+
/// A barrier will block `n`-1 threads which call [`wait`] and then wake up
82+
/// all threads at once when the `n`th thread calls [`wait`].
83+
///
84+
/// [`wait`]: #method.wait
85+
///
86+
/// # Examples
87+
///
88+
/// ```
89+
/// use std::sync::Barrier;
90+
///
91+
/// let barrier = Barrier::new(10);
92+
/// ```
7093
#[stable(feature = "rust1", since = "1.0.0")]
7194
pub fn new(n: usize) -> Barrier {
7295
Barrier {
@@ -84,10 +107,37 @@ impl Barrier {
84107
/// Barriers are re-usable after all threads have rendezvoused once, and can
85108
/// be used continuously.
86109
///
87-
/// A single (arbitrary) thread will receive a `BarrierWaitResult` that
88-
/// returns `true` from `is_leader` when returning from this function, and
110+
/// A single (arbitrary) thread will receive a [`BarrierWaitResult`] that
111+
/// returns `true` from [`is_leader`] when returning from this function, and
89112
/// all other threads will receive a result that will return `false` from
90-
/// `is_leader`
113+
/// [`is_leader`].
114+
///
115+
/// [`BarrierWaitResult`]: struct.BarrierWaitResult.html
116+
/// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader
117+
///
118+
/// # Examples
119+
///
120+
/// ```
121+
/// use std::sync::{Arc, Barrier};
122+
/// use std::thread;
123+
///
124+
/// let mut handles = Vec::with_capacity(10);
125+
/// let barrier = Arc::new(Barrier::new(10));
126+
/// for _ in 0..10 {
127+
/// let c = barrier.clone();
128+
/// // The same messages will be printed together.
129+
/// // You will NOT see any interleaving.
130+
/// handles.push(thread::spawn(move|| {
131+
/// println!("before wait");
132+
/// c.wait();
133+
/// println!("after wait");
134+
/// }));
135+
/// }
136+
/// // Wait for other threads to finish.
137+
/// for handle in handles {
138+
/// handle.join().unwrap();
139+
/// }
140+
/// ```
91141
#[stable(feature = "rust1", since = "1.0.0")]
92142
pub fn wait(&self) -> BarrierWaitResult {
93143
let mut lock = self.lock.lock().unwrap();
@@ -120,10 +170,22 @@ impl fmt::Debug for BarrierWaitResult {
120170
}
121171

122172
impl BarrierWaitResult {
123-
/// Returns whether this thread from `wait` is the "leader thread".
173+
/// Returns whether this thread from [`wait`] is the "leader thread".
124174
///
125175
/// Only one thread will have `true` returned from their result, all other
126176
/// threads will have `false` returned.
177+
///
178+
/// [`wait`]: struct.Barrier.html#method.wait
179+
///
180+
/// # Examples
181+
///
182+
/// ```
183+
/// use std::sync::Barrier;
184+
///
185+
/// let barrier = Barrier::new(1);
186+
/// let barrier_wait_result = barrier.wait();
187+
/// println!("{:?}", barrier_wait_result.is_leader());
188+
/// ```
127189
#[stable(feature = "rust1", since = "1.0.0")]
128190
pub fn is_leader(&self) -> bool { self.0 }
129191
}

0 commit comments

Comments
 (0)