@@ -107,8 +107,8 @@ use crate::sys::locks as sys;
107107/// *guard += 1;
108108/// ```
109109///
110- /// It is sometimes necessary to manually drop the mutex guard to unlock it
111- /// sooner than the end of the enclosing scope.
110+ /// It is sometimes necessary to manually drop the mutex guard or to create an inner scope
111+ /// to unlock it sooner than the end of the enclosing scope.
112112///
113113/// ```
114114/// use std::sync::{Arc, Mutex};
@@ -125,11 +125,18 @@ use crate::sys::locks as sys;
125125/// let res_mutex_clone = Arc::clone(&res_mutex);
126126///
127127/// threads.push(thread::spawn(move || {
128- /// let mut data = data_mutex_clone.lock().unwrap();
129- /// // This is the result of some important and long-ish work.
130- /// let result = data.iter().fold(0, |acc, x| acc + x * 2);
131- /// data.push(result);
132- /// drop(data);
128+ /// // Here we use a block to limit the lifetime of the lock guard.
129+ /// let result = {
130+ /// let mut data = data_mutex_clone.lock().unwrap();
131+ /// // This is the result of some important and long-ish work.
132+ /// let result = data.iter().fold(0, |acc, x| acc + x * 2);
133+ /// data.push(result);
134+ /// result
135+ /// // The mutex guard gets dropped here, together with any other values
136+ /// // created in the critical section.
137+ /// };
138+ /// // The guard created here is a temporary dropped at the end of the statement, i.e.
139+ /// // the lock would not remain being held even if the thread did some additional work.
133140/// *res_mutex_clone.lock().unwrap() += result;
134141/// }));
135142/// });
@@ -146,6 +153,8 @@ use crate::sys::locks as sys;
146153/// // It's even more important here than in the threads because we `.join` the
147154/// // threads after that. If we had not dropped the mutex guard, a thread could
148155/// // be waiting forever for it, causing a deadlock.
156+ /// // As in the threads a block could have been used instead of calling the
157+ /// // `drop` function.
149158/// drop(data);
150159/// // Here the mutex guard is not assigned to a variable and so, even if the
151160/// // scope does not end after this line, the mutex is still released: there is
@@ -160,6 +169,7 @@ use crate::sys::locks as sys;
160169///
161170/// assert_eq!(*res_mutex.lock().unwrap(), 800);
162171/// ```
172+ ///
163173#[ stable( feature = "rust1" , since = "1.0.0" ) ]
164174#[ cfg_attr( not( test) , rustc_diagnostic_item = "Mutex" ) ]
165175pub struct Mutex < T : ?Sized > {
0 commit comments