@@ -18,12 +18,57 @@ use time::Duration;
1818
1919/// A type indicating whether a timed wait on a condition variable returned
2020/// due to a time out or not.
21+ ///
22+ /// It is returned by the [`wait_timeout`] method.
23+ ///
24+ /// [`wait_timeout`]: struct.Condvar.html#method.wait_timeout
2125#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
2226#[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
2327pub struct WaitTimeoutResult ( bool ) ;
2428
2529impl WaitTimeoutResult {
2630 /// Returns whether the wait was known to have timed out.
31+ ///
32+ /// # Examples
33+ ///
34+ /// This example spawns a thread which will update the boolean value and
35+ /// then wait 100 milliseconds before notifying the condvar.
36+ ///
37+ /// The main thread will wait with a timeout on the condvar and then leave
38+ /// once the boolean has been updated and notified.
39+ ///
40+ /// ```
41+ /// use std::sync::{Arc, Mutex, Condvar};
42+ /// use std::thread;
43+ /// use std::time::Duration;
44+ ///
45+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
46+ /// let pair2 = pair.clone();
47+ ///
48+ /// thread::spawn(move|| {
49+ /// let &(ref lock, ref cvar) = &*pair2;
50+ /// let mut started = lock.lock().unwrap();
51+ /// // We update the boolean value.
52+ /// *started = true;
53+ /// // Let's wait 20 milliseconds before notifying the condvar.
54+ /// thread::sleep(Duration::from_millis(20));
55+ /// cvar.notify_one();
56+ /// });
57+ ///
58+ /// // Wait for the thread to start up.
59+ /// let &(ref lock, ref cvar) = &*pair;
60+ /// let mut started = lock.lock().unwrap();
61+ /// loop {
62+ /// // Let's put a timeout on the condvar's wait.
63+ /// let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap();
64+ /// // 10 milliseconds have passed, or maybe the value changed!
65+ /// started = result.0;
66+ /// if *started == true {
67+ /// // We received the notification and the value has been updated, we can leave.
68+ /// break
69+ /// }
70+ /// }
71+ /// ```
2772 #[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
2873 pub fn timed_out ( & self ) -> bool {
2974 self . 0
@@ -55,15 +100,16 @@ impl WaitTimeoutResult {
55100/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
56101/// let pair2 = pair.clone();
57102///
58- /// // Inside of our lock, spawn a new thread, and then wait for it to start
103+ /// // Inside of our lock, spawn a new thread, and then wait for it to start.
59104/// thread::spawn(move|| {
60105/// let &(ref lock, ref cvar) = &*pair2;
61106/// let mut started = lock.lock().unwrap();
62107/// *started = true;
108+ /// // We notify the condvar that the value has changed.
63109/// cvar.notify_one();
64110/// });
65111///
66- /// // wait for the thread to start up
112+ /// // Wait for the thread to start up.
67113/// let &(ref lock, ref cvar) = &*pair;
68114/// let mut started = lock.lock().unwrap();
69115/// while !*started {
@@ -79,6 +125,14 @@ pub struct Condvar {
79125impl Condvar {
80126 /// Creates a new condition variable which is ready to be waited on and
81127 /// notified.
128+ ///
129+ /// # Examples
130+ ///
131+ /// ```
132+ /// use std::sync::Condvar;
133+ ///
134+ /// let condvar = Condvar::new();
135+ /// ```
82136 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
83137 pub fn new ( ) -> Condvar {
84138 let mut c = Condvar {
@@ -95,10 +149,10 @@ impl Condvar {
95149 /// notification.
96150 ///
97151 /// This function will atomically unlock the mutex specified (represented by
98- /// `mutex_guard `) and block the current thread. This means that any calls
99- /// to `notify_* ()` which happen logically after the mutex is unlocked are
100- /// candidates to wake this thread up. When this function call returns, the
101- /// lock specified will have been re-acquired.
152+ /// `guard `) and block the current thread. This means that any calls
153+ /// to [`notify_one ()`] or [`notify_all()`] which happen logically after the
154+ /// mutex is unlocked are candidates to wake this thread up. When this
155+ /// function call returns, the lock specified will have been re-acquired.
102156 ///
103157 /// Note that this function is susceptible to spurious wakeups. Condition
104158 /// variables normally have a boolean predicate associated with them, and
@@ -109,14 +163,46 @@ impl Condvar {
109163 ///
110164 /// This function will return an error if the mutex being waited on is
111165 /// poisoned when this thread re-acquires the lock. For more information,
112- /// see information about poisoning on the Mutex type.
166+ /// see information about [ poisoning] on the [` Mutex`] type.
113167 ///
114168 /// # Panics
115169 ///
116- /// This function will `panic!()` if it is used with more than one mutex
170+ /// This function will [ `panic!()`] if it is used with more than one mutex
117171 /// over time. Each condition variable is dynamically bound to exactly one
118172 /// mutex to ensure defined behavior across platforms. If this functionality
119173 /// is not desired, then unsafe primitives in `sys` are provided.
174+ ///
175+ /// [`notify_one()`]: #method.notify_one
176+ /// [`notify_all()`]: #method.notify_all
177+ /// [poisoning]: ../sync/struct.Mutex.html#poisoning
178+ /// [`Mutex`]: ../sync/struct.Mutex.html
179+ /// [`panic!()`]: ../../std/macro.panic.html
180+ ///
181+ /// # Examples
182+ ///
183+ /// ```
184+ /// use std::sync::{Arc, Mutex, Condvar};
185+ /// use std::thread;
186+ ///
187+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
188+ /// let pair2 = pair.clone();
189+ ///
190+ /// thread::spawn(move|| {
191+ /// let &(ref lock, ref cvar) = &*pair2;
192+ /// let mut started = lock.lock().unwrap();
193+ /// *started = true;
194+ /// // We notify the condvar that the value has changed.
195+ /// cvar.notify_one();
196+ /// });
197+ ///
198+ /// // Wait for the thread to start up.
199+ /// let &(ref lock, ref cvar) = &*pair;
200+ /// let mut started = lock.lock().unwrap();
201+ /// // As long as the value inside the `Mutex` is false, we wait.
202+ /// while !*started {
203+ /// started = cvar.wait(started).unwrap();
204+ /// }
205+ /// ```
120206 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
121207 pub fn wait < ' a , T > ( & self , guard : MutexGuard < ' a , T > )
122208 -> LockResult < MutexGuard < ' a , T > > {
@@ -136,7 +222,7 @@ impl Condvar {
136222 /// Waits on this condition variable for a notification, timing out after a
137223 /// specified duration.
138224 ///
139- /// The semantics of this function are equivalent to `wait()`
225+ /// The semantics of this function are equivalent to [ `wait`]
140226 /// except that the thread will be blocked for roughly no longer
141227 /// than `ms` milliseconds. This method should not be used for
142228 /// precise timing due to anomalies such as preemption or platform
@@ -150,8 +236,42 @@ impl Condvar {
150236 /// The returned boolean is `false` only if the timeout is known
151237 /// to have elapsed.
152238 ///
153- /// Like `wait`, the lock specified will be re-acquired when this function
239+ /// Like [ `wait`] , the lock specified will be re-acquired when this function
154240 /// returns, regardless of whether the timeout elapsed or not.
241+ ///
242+ /// [`wait`]: #method.wait
243+ ///
244+ /// # Examples
245+ ///
246+ /// ```
247+ /// use std::sync::{Arc, Mutex, Condvar};
248+ /// use std::thread;
249+ ///
250+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
251+ /// let pair2 = pair.clone();
252+ ///
253+ /// thread::spawn(move|| {
254+ /// let &(ref lock, ref cvar) = &*pair2;
255+ /// let mut started = lock.lock().unwrap();
256+ /// *started = true;
257+ /// // We notify the condvar that the value has changed.
258+ /// cvar.notify_one();
259+ /// });
260+ ///
261+ /// // Wait for the thread to start up.
262+ /// let &(ref lock, ref cvar) = &*pair;
263+ /// let mut started = lock.lock().unwrap();
264+ /// // As long as the value inside the `Mutex` is false, we wait.
265+ /// loop {
266+ /// let result = cvar.wait_timeout_ms(started, 10).unwrap();
267+ /// // 10 milliseconds have passed, or maybe the value changed!
268+ /// started = result.0;
269+ /// if *started == true {
270+ /// // We received the notification and the value has been updated, we can leave.
271+ /// break
272+ /// }
273+ /// }
274+ /// ```
155275 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
156276 #[ rustc_deprecated( since = "1.6.0" , reason = "replaced by `std::sync::Condvar::wait_timeout`" ) ]
157277 pub fn wait_timeout_ms < ' a , T > ( & self , guard : MutexGuard < ' a , T > , ms : u32 )
@@ -165,7 +285,7 @@ impl Condvar {
165285 /// Waits on this condition variable for a notification, timing out after a
166286 /// specified duration.
167287 ///
168- /// The semantics of this function are equivalent to `wait()` except that
288+ /// The semantics of this function are equivalent to [ `wait`] except that
169289 /// the thread will be blocked for roughly no longer than `dur`. This
170290 /// method should not be used for precise timing due to anomalies such as
171291 /// preemption or platform differences that may not cause the maximum
@@ -175,11 +295,47 @@ impl Condvar {
175295 /// measured with a monotonic clock, and not affected by the changes made to
176296 /// the system time.
177297 ///
178- /// The returned `WaitTimeoutResult` value indicates if the timeout is
298+ /// The returned [ `WaitTimeoutResult`] value indicates if the timeout is
179299 /// known to have elapsed.
180300 ///
181- /// Like `wait`, the lock specified will be re-acquired when this function
301+ /// Like [ `wait`] , the lock specified will be re-acquired when this function
182302 /// returns, regardless of whether the timeout elapsed or not.
303+ ///
304+ /// [`wait`]: #method.wait
305+ /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
306+ ///
307+ /// # Examples
308+ ///
309+ /// ```
310+ /// use std::sync::{Arc, Mutex, Condvar};
311+ /// use std::thread;
312+ /// use std::time::Duration;
313+ ///
314+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
315+ /// let pair2 = pair.clone();
316+ ///
317+ /// thread::spawn(move|| {
318+ /// let &(ref lock, ref cvar) = &*pair2;
319+ /// let mut started = lock.lock().unwrap();
320+ /// *started = true;
321+ /// // We notify the condvar that the value has changed.
322+ /// cvar.notify_one();
323+ /// });
324+ ///
325+ /// // wait for the thread to start up
326+ /// let &(ref lock, ref cvar) = &*pair;
327+ /// let mut started = lock.lock().unwrap();
328+ /// // as long as the value inside the `Mutex` is false, we wait
329+ /// loop {
330+ /// let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap();
331+ /// // 10 milliseconds have passed, or maybe the value changed!
332+ /// started = result.0;
333+ /// if *started == true {
334+ /// // We received the notification and the value has been updated, we can leave.
335+ /// break
336+ /// }
337+ /// }
338+ /// ```
183339 #[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
184340 pub fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > ,
185341 dur : Duration )
@@ -200,10 +356,40 @@ impl Condvar {
200356 /// Wakes up one blocked thread on this condvar.
201357 ///
202358 /// If there is a blocked thread on this condition variable, then it will
203- /// be woken up from its call to `wait` or `wait_timeout`. Calls to
359+ /// be woken up from its call to [ `wait`] or [ `wait_timeout`] . Calls to
204360 /// `notify_one` are not buffered in any way.
205361 ///
206- /// To wake up all threads, see `notify_all()`.
362+ /// To wake up all threads, see [`notify_all()`].
363+ ///
364+ /// [`wait`]: #method.wait
365+ /// [`wait_timeout`]: #method.wait_timeout
366+ /// [`notify_all()`]: #method.notify_all
367+ ///
368+ /// # Examples
369+ ///
370+ /// ```
371+ /// use std::sync::{Arc, Mutex, Condvar};
372+ /// use std::thread;
373+ ///
374+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
375+ /// let pair2 = pair.clone();
376+ ///
377+ /// thread::spawn(move|| {
378+ /// let &(ref lock, ref cvar) = &*pair2;
379+ /// let mut started = lock.lock().unwrap();
380+ /// *started = true;
381+ /// // We notify the condvar that the value has changed.
382+ /// cvar.notify_one();
383+ /// });
384+ ///
385+ /// // Wait for the thread to start up.
386+ /// let &(ref lock, ref cvar) = &*pair;
387+ /// let mut started = lock.lock().unwrap();
388+ /// // As long as the value inside the `Mutex` is false, we wait.
389+ /// while !*started {
390+ /// started = cvar.wait(started).unwrap();
391+ /// }
392+ /// ```
207393 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
208394 pub fn notify_one ( & self ) {
209395 unsafe { self . inner . notify_one ( ) }
@@ -215,7 +401,35 @@ impl Condvar {
215401 /// variable are awoken. Calls to `notify_all()` are not buffered in any
216402 /// way.
217403 ///
218- /// To wake up only one thread, see `notify_one()`.
404+ /// To wake up only one thread, see [`notify_one()`].
405+ ///
406+ /// [`notify_one()`]: #method.notify_one
407+ ///
408+ /// # Examples
409+ ///
410+ /// ```
411+ /// use std::sync::{Arc, Mutex, Condvar};
412+ /// use std::thread;
413+ ///
414+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
415+ /// let pair2 = pair.clone();
416+ ///
417+ /// thread::spawn(move|| {
418+ /// let &(ref lock, ref cvar) = &*pair2;
419+ /// let mut started = lock.lock().unwrap();
420+ /// *started = true;
421+ /// // We notify the condvar that the value has changed.
422+ /// cvar.notify_all();
423+ /// });
424+ ///
425+ /// // Wait for the thread to start up.
426+ /// let &(ref lock, ref cvar) = &*pair;
427+ /// let mut started = lock.lock().unwrap();
428+ /// // As long as the value inside the `Mutex` is false, we wait.
429+ /// while !*started {
430+ /// started = cvar.wait(started).unwrap();
431+ /// }
432+ /// ```
219433 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
220434 pub fn notify_all ( & self ) {
221435 unsafe { self . inner . notify_all ( ) }
0 commit comments