Skip to content

Commit 713b635

Browse files
committed
update mutex docs for send & sync
1 parent 3139ff0 commit 713b635

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

library/std/src/sync/mutex.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,51 @@ pub struct Mutex<T: ?Sized> {
183183

184184
// these are the only places where `T: Send` matters; all other
185185
// functionality works fine on a single thread.
186+
187+
/// SAFETY
188+
///
189+
/// The `Send` and `Sync` implementations for `Mutex` ensure that it is safe to
190+
/// share instances of `Mutex` between threads when the protected data is also
191+
/// thread-safe. The following explains the safety guarantees:
192+
///
193+
/// - `Send` is implemented for `Mutex<T>` if and only if `T` is also `Send`.
194+
/// This guarantees that `Mutex<T>` can be safely transferred between threads,
195+
/// and `T` can be sent across thread boundaries. This is crucial for allowing
196+
/// safe access to the protected data from multiple threads.
197+
///
198+
/// - `Sync` is implemented for `Mutex<T>` if and only if `T` is both `Send` and
199+
/// `Sync`. This ensures that `Mutex<T>` can be safely shared between threads
200+
/// without requiring further synchronization, assuming `T` can be sent across
201+
/// thread boundaries. It guarantees that multiple threads can safely access the
202+
/// protected data concurrently without data races.
203+
///
204+
/// It's important to note that `Mutex` can be `Sync` even if its inner type `T`
205+
/// is not `Sync` itself. This is because `Mutex` provides a safe interface for
206+
/// accessing `T` through locking mechanisms, ensuring that only one thread can
207+
/// access `T` at a time.
208+
///
186209
#[stable(feature = "rust1", since = "1.0.0")]
187210
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
211+
212+
/// SAFETY
213+
///
214+
/// The `Send` and `Sync` implementations for `MutexGuard` ensure that it is
215+
/// safe to share instances of `MutexGuard` between threads when the protected
216+
/// data is also thread-safe. The following explains the safety guarantees:
217+
///
218+
/// - `MutexGuard` is not `Send` because it represents exclusive access to the
219+
/// data protected by `Mutex`, and sending it to another thread could lead to
220+
/// data races or other unsafe behavior, violating the mutual exclusion property
221+
/// provided by `Mutex`.
222+
///
223+
/// - `Sync` is implemented for `MutexGuard` if and only if `T` is `Send`. This
224+
/// ensures that `MutexGuard` can be safely shared between threads if the
225+
/// protected data can be sent across thread boundaries. It guarantees that
226+
/// multiple threads can safely access the protected data concurrently without
227+
/// data races.
228+
///
188229
#[stable(feature = "rust1", since = "1.0.0")]
189-
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
230+
unsafe impl<T: ?Sized + Send> Sync for MutexGuard<T> {}
190231

191232
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
192233
/// dropped (falls out of scope), the lock will be unlocked.

0 commit comments

Comments
 (0)