@@ -14,6 +14,8 @@ use sync::{Mutex, Condvar};
14
14
/// A barrier enables multiple threads to synchronize the beginning
15
15
/// of some computation.
16
16
///
17
+ /// # Examples
18
+ ///
17
19
/// ```
18
20
/// use std::sync::{Arc, Barrier};
19
21
/// use std::thread;
@@ -50,8 +52,19 @@ struct BarrierState {
50
52
51
53
/// A result returned from wait.
52
54
///
53
- /// Currently this opaque structure only has one method, `.is_leader()`. Only
55
+ /// Currently this opaque structure only has one method, [ `.is_leader()`] . Only
54
56
/// 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
+ /// ```
55
68
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
56
69
pub struct BarrierWaitResult ( bool ) ;
57
70
@@ -65,8 +78,18 @@ impl fmt::Debug for Barrier {
65
78
impl Barrier {
66
79
/// Creates a new barrier that can block a given number of threads.
67
80
///
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
+ /// ```
70
93
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
71
94
pub fn new ( n : usize ) -> Barrier {
72
95
Barrier {
@@ -84,10 +107,37 @@ impl Barrier {
84
107
/// Barriers are re-usable after all threads have rendezvoused once, and can
85
108
/// be used continuously.
86
109
///
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
89
112
/// 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
+ /// ```
91
141
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
92
142
pub fn wait ( & self ) -> BarrierWaitResult {
93
143
let mut lock = self . lock . lock ( ) . unwrap ( ) ;
@@ -120,10 +170,22 @@ impl fmt::Debug for BarrierWaitResult {
120
170
}
121
171
122
172
impl BarrierWaitResult {
123
- /// Returns whether this thread from `wait` is the "leader thread".
173
+ /// Returns whether this thread from [ `wait`] is the "leader thread".
124
174
///
125
175
/// Only one thread will have `true` returned from their result, all other
126
176
/// 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
+ /// ```
127
189
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
128
190
pub fn is_leader ( & self ) -> bool { self . 0 }
129
191
}
0 commit comments