@@ -155,6 +155,14 @@ impl<'a, T: ?Sized> !marker::Send for MutexGuard<'a, T> {}
155
155
156
156
impl < T > Mutex < T > {
157
157
/// Creates a new mutex in an unlocked state ready for use.
158
+ ///
159
+ /// # Examples
160
+ ///
161
+ /// ```
162
+ /// use std::sync::Mutex;
163
+ ///
164
+ /// let mutex = Mutex::new(0);
165
+ /// ```
158
166
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
159
167
pub fn new ( t : T ) -> Mutex < T > {
160
168
let mut m = Mutex {
@@ -190,6 +198,21 @@ impl<T: ?Sized> Mutex<T> {
190
198
///
191
199
/// This function might panic when called if the lock is already held by
192
200
/// the current thread.
201
+ ///
202
+ /// # Examples
203
+ ///
204
+ /// ```
205
+ /// use std::sync::{Arc, Mutex};
206
+ /// use std::thread;
207
+ ///
208
+ /// let mutex = Arc::new(Mutex::new(0));
209
+ /// let c_mutex = mutex.clone();
210
+ ///
211
+ /// thread::spawn(move || {
212
+ /// *c_mutex.lock().unwrap() = 10;
213
+ /// }).join().expect("thread::spawn failed");
214
+ /// assert_eq!(*mutex.lock().unwrap(), 10);
215
+ /// ```
193
216
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
194
217
pub fn lock ( & self ) -> LockResult < MutexGuard < T > > {
195
218
unsafe {
@@ -211,6 +234,26 @@ impl<T: ?Sized> Mutex<T> {
211
234
/// If another user of this mutex panicked while holding the mutex, then
212
235
/// this call will return failure if the mutex would otherwise be
213
236
/// acquired.
237
+ ///
238
+ /// # Examples
239
+ ///
240
+ /// ```
241
+ /// use std::sync::{Arc, Mutex};
242
+ /// use std::thread;
243
+ ///
244
+ /// let mutex = Arc::new(Mutex::new(0));
245
+ /// let c_mutex = mutex.clone();
246
+ ///
247
+ /// thread::spawn(move || {
248
+ /// let mut lock = c_mutex.try_lock();
249
+ /// if let Ok(ref mut mutex) = lock {
250
+ /// **mutex = 10;
251
+ /// } else {
252
+ /// println!("try_lock failed");
253
+ /// }
254
+ /// }).join().expect("thread::spawn failed");
255
+ /// assert_eq!(*mutex.lock().unwrap(), 10);
256
+ /// ```
214
257
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
215
258
pub fn try_lock ( & self ) -> TryLockResult < MutexGuard < T > > {
216
259
unsafe {
@@ -225,8 +268,24 @@ impl<T: ?Sized> Mutex<T> {
225
268
/// Determines whether the lock is poisoned.
226
269
///
227
270
/// If another thread is active, the lock can still become poisoned at any
228
- /// time. You should not trust a `false` value for program correctness
271
+ /// time. You should not trust a `false` value for program correctness
229
272
/// without additional synchronization.
273
+ ///
274
+ /// # Examples
275
+ ///
276
+ /// ```
277
+ /// use std::sync::{Arc, Mutex};
278
+ /// use std::thread;
279
+ ///
280
+ /// let mutex = Arc::new(Mutex::new(0));
281
+ /// let c_mutex = mutex.clone();
282
+ ///
283
+ /// let _ = thread::spawn(move || {
284
+ /// let _lock = c_mutex.lock().unwrap();
285
+ /// panic!(); // the mutex gets poisoned
286
+ /// }).join();
287
+ /// assert_eq!(mutex.is_poisoned(), true);
288
+ /// ```
230
289
#[ inline]
231
290
#[ stable( feature = "sync_poison" , since = "1.2.0" ) ]
232
291
pub fn is_poisoned ( & self ) -> bool {
@@ -239,6 +298,15 @@ impl<T: ?Sized> Mutex<T> {
239
298
///
240
299
/// If another user of this mutex panicked while holding the mutex, then
241
300
/// this call will return an error instead.
301
+ ///
302
+ /// # Examples
303
+ ///
304
+ /// ```
305
+ /// use std::sync::Mutex;
306
+ ///
307
+ /// let mutex = Mutex::new(0);
308
+ /// assert_eq!(mutex.into_inner().unwrap(), 0);
309
+ /// ```
242
310
#[ stable( feature = "mutex_into_inner" , since = "1.6.0" ) ]
243
311
pub fn into_inner ( self ) -> LockResult < T > where T : Sized {
244
312
// We know statically that there are no outstanding references to
@@ -270,6 +338,16 @@ impl<T: ?Sized> Mutex<T> {
270
338
///
271
339
/// If another user of this mutex panicked while holding the mutex, then
272
340
/// this call will return an error instead.
341
+ ///
342
+ /// # Examples
343
+ ///
344
+ /// ```
345
+ /// use std::sync::Mutex;
346
+ ///
347
+ /// let mut mutex = Mutex::new(0);
348
+ /// *mutex.get_mut().unwrap() = 10;
349
+ /// assert_eq!(*mutex.lock().unwrap(), 10);
350
+ /// ```
273
351
#[ stable( feature = "mutex_get_mut" , since = "1.6.0" ) ]
274
352
pub fn get_mut ( & mut self ) -> LockResult < & mut T > {
275
353
// We know statically that there are no other references to `self`, so
0 commit comments