@@ -18,12 +18,57 @@ use time::Duration;
18
18
19
19
/// A type indicating whether a timed wait on a condition variable returned
20
20
/// 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
21
25
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
22
26
#[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
23
27
pub struct WaitTimeoutResult ( bool ) ;
24
28
25
29
impl WaitTimeoutResult {
26
30
/// 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
+ /// ```
27
72
#[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
28
73
pub fn timed_out ( & self ) -> bool {
29
74
self . 0
@@ -55,15 +100,16 @@ impl WaitTimeoutResult {
55
100
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
56
101
/// let pair2 = pair.clone();
57
102
///
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.
59
104
/// thread::spawn(move|| {
60
105
/// let &(ref lock, ref cvar) = &*pair2;
61
106
/// let mut started = lock.lock().unwrap();
62
107
/// *started = true;
108
+ /// // We notify the condvar that the value has changed.
63
109
/// cvar.notify_one();
64
110
/// });
65
111
///
66
- /// // wait for the thread to start up
112
+ /// // Wait for the thread to start up.
67
113
/// let &(ref lock, ref cvar) = &*pair;
68
114
/// let mut started = lock.lock().unwrap();
69
115
/// while !*started {
@@ -79,6 +125,14 @@ pub struct Condvar {
79
125
impl Condvar {
80
126
/// Creates a new condition variable which is ready to be waited on and
81
127
/// notified.
128
+ ///
129
+ /// # Examples
130
+ ///
131
+ /// ```
132
+ /// use std::sync::Condvar;
133
+ ///
134
+ /// let condvar = Condvar::new();
135
+ /// ```
82
136
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
83
137
pub fn new ( ) -> Condvar {
84
138
let mut c = Condvar {
@@ -95,10 +149,10 @@ impl Condvar {
95
149
/// notification.
96
150
///
97
151
/// 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.
102
156
///
103
157
/// Note that this function is susceptible to spurious wakeups. Condition
104
158
/// variables normally have a boolean predicate associated with them, and
@@ -109,14 +163,46 @@ impl Condvar {
109
163
///
110
164
/// This function will return an error if the mutex being waited on is
111
165
/// 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.
113
167
///
114
168
/// # Panics
115
169
///
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
117
171
/// over time. Each condition variable is dynamically bound to exactly one
118
172
/// mutex to ensure defined behavior across platforms. If this functionality
119
173
/// 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
+ /// ```
120
206
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
121
207
pub fn wait < ' a , T > ( & self , guard : MutexGuard < ' a , T > )
122
208
-> LockResult < MutexGuard < ' a , T > > {
@@ -136,7 +222,7 @@ impl Condvar {
136
222
/// Waits on this condition variable for a notification, timing out after a
137
223
/// specified duration.
138
224
///
139
- /// The semantics of this function are equivalent to `wait()`
225
+ /// The semantics of this function are equivalent to [ `wait`]
140
226
/// except that the thread will be blocked for roughly no longer
141
227
/// than `ms` milliseconds. This method should not be used for
142
228
/// precise timing due to anomalies such as preemption or platform
@@ -150,8 +236,42 @@ impl Condvar {
150
236
/// The returned boolean is `false` only if the timeout is known
151
237
/// to have elapsed.
152
238
///
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
154
240
/// 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
+ /// ```
155
275
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
156
276
#[ rustc_deprecated( since = "1.6.0" , reason = "replaced by `std::sync::Condvar::wait_timeout`" ) ]
157
277
pub fn wait_timeout_ms < ' a , T > ( & self , guard : MutexGuard < ' a , T > , ms : u32 )
@@ -165,7 +285,7 @@ impl Condvar {
165
285
/// Waits on this condition variable for a notification, timing out after a
166
286
/// specified duration.
167
287
///
168
- /// The semantics of this function are equivalent to `wait()` except that
288
+ /// The semantics of this function are equivalent to [ `wait`] except that
169
289
/// the thread will be blocked for roughly no longer than `dur`. This
170
290
/// method should not be used for precise timing due to anomalies such as
171
291
/// preemption or platform differences that may not cause the maximum
@@ -175,11 +295,47 @@ impl Condvar {
175
295
/// measured with a monotonic clock, and not affected by the changes made to
176
296
/// the system time.
177
297
///
178
- /// The returned `WaitTimeoutResult` value indicates if the timeout is
298
+ /// The returned [ `WaitTimeoutResult`] value indicates if the timeout is
179
299
/// known to have elapsed.
180
300
///
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
182
302
/// 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
+ /// ```
183
339
#[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
184
340
pub fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > ,
185
341
dur : Duration )
@@ -200,10 +356,40 @@ impl Condvar {
200
356
/// Wakes up one blocked thread on this condvar.
201
357
///
202
358
/// 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
204
360
/// `notify_one` are not buffered in any way.
205
361
///
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
+ /// ```
207
393
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
208
394
pub fn notify_one ( & self ) {
209
395
unsafe { self . inner . notify_one ( ) }
@@ -215,7 +401,35 @@ impl Condvar {
215
401
/// variable are awoken. Calls to `notify_all()` are not buffered in any
216
402
/// way.
217
403
///
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
+ /// ```
219
433
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
220
434
pub fn notify_all ( & self ) {
221
435
unsafe { self . inner . notify_all ( ) }
0 commit comments