Skip to content

Commit e92e5fd

Browse files
committed
add Mutex::unlock
1 parent 3f5aee2 commit e92e5fd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/std/src/sync/mutex.rs

+20
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ impl<T> Mutex<T> {
219219
data: UnsafeCell::new(t),
220220
}
221221
}
222+
223+
/// Immediately drops the guard, and consequently unlocks the mutex.
224+
///
225+
/// This function is equivalent to calling [`drop`] on the guard but is more self-documenting.
226+
/// Alternately, the guard will be automatically dropped when it goes out of scope.
227+
///
228+
/// ```
229+
/// #![feature(mutex_unlock)]
230+
///
231+
/// use std::sync::Mutex;
232+
/// let mutex = Mutex::new(0);
233+
///
234+
/// let mut guard = mutex.lock().unwrap();
235+
/// *guard += 20;
236+
/// Mutex::unlock(guard);
237+
/// ```
238+
#[unstable(feature = "mutex_unlock", issue = "81872")]
239+
pub fn unlock(guard: MutexGuard<'_, T>) {
240+
drop(guard);
241+
}
222242
}
223243

224244
impl<T: ?Sized> Mutex<T> {

0 commit comments

Comments
 (0)