update mutex docs for send & sync#123225
update mutex docs for send & sync#123225Psalmuel01 wants to merge 6 commits intorust-lang:masterfrom
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
library/std/src/sync/mutex.rs
Outdated
| /// - `Sync` is implemented for `Mutex<T>` if and only if `T` is both `Send` and | ||
| /// `Sync`. This ensures that `Mutex<T>` can be safely shared between threads |
There was a problem hiding this comment.
That's not true. Since passing around a &Mutex<T> is basically the same as passing a &mut T, Send suffices.
There was a problem hiding this comment.
oh yeahh, thanks for that. will correct now
library/std/src/sync/mutex.rs
Outdated
| /// safe to share instances of `MutexGuard` between threads when the protected | ||
| /// data is also thread-safe. The following explains the safety guarantees: | ||
| /// | ||
| /// - `MutexGuard` is not `Send` because it represents exclusive access to the |
There was a problem hiding this comment.
This is a platform limitation not intrinsically implied by the concept of mutual exclusion.
library/std/src/sync/mutex.rs
Outdated
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} | ||
|
|
||
| /// SAFETY |
There was a problem hiding this comment.
Why is this describing MutexGuard but applied to an impl for Mutex?
There was a problem hiding this comment.
oh that must be an oversight, will see to that
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
|
Hi, I'm the author of original issue. It's a good idea to link the issue you're solving in your PR: Read more on How to link PR to the issue |
IoaNNUwU
left a comment
There was a problem hiding this comment.
Thanks for your work, I think this could be improved.
library/std/src/sync/mutex.rs
Outdated
| /// | ||
| /// The `Send` and `Sync` implementations for `Mutex` ensure that it is safe to | ||
| /// share instances of `Mutex` between threads when the protected data is also | ||
| /// thread-safe. The following explains the safety guarantees: |
There was a problem hiding this comment.
This part explains what Sync & Send do, which is already explained in Sync & Send doc.
I think SAFETY comments should be as short as possible, so this part should be removed.
library/std/src/sync/mutex.rs
Outdated
| /// - `Send` is implemented for `Mutex<T>` if and only if `T` is also `Send`. | ||
| /// This guarantees that `Mutex<T>` can be safely transferred between threads, | ||
| /// and `T` can be sent across thread boundaries. This is crucial for allowing | ||
| /// safe access to the protected data from multiple threads. |
There was a problem hiding this comment.
I think this statement is obvious, because Send doc states:
Types that can be transferred across thread boundaries.
I'd much prefer to see exactly why is it crucial for T to be Send. So I think this should be changed to something along the lines of:
Mutexis container which wrapsT, so it's necessary forTto beSendto safely sendMutexto another thread.
library/std/src/sync/mutex.rs
Outdated
| /// This ensures that `Mutex<T>` can be safely shared between threads | ||
| /// without requiring further synchronization, assuming `T` can be sent across | ||
| /// thread boundaries. It guarantees that multiple threads can safely access the | ||
| /// protected data concurrently without data races. |
There was a problem hiding this comment.
This describes Sync, so it should be probably moved to impl Sync for Mutex.
Also, I'm not a big fan of is basically the same as terminology in the docs. I would much prefer to see something like:
Mutex<T>provides mutable access toTto one thread at the time, yet stillThas to beSendbecause it's not OK forNon-Sendstructures to be accessed this way.
One example of this is
Rc(non-atomic reference counted smart pointer), which is notSend. We can have multiple copies ofRcpointing to same heap allocation with non-atomic reference count.Mutex<Rc<_>>would only protect one copy ofRcfrom shared access, making it vulnerable to data-races.
| /// It's important to note that `Mutex` can be `Sync` even if its inner type `T` | ||
| /// is not `Sync` itself. This is because `Mutex` provides a safe interface for | ||
| /// accessing `T` through locking mechanisms, ensuring that only one thread can | ||
| /// access `T` at a time. |
There was a problem hiding this comment.
This note is great, but it should also be moved to impl Sync for Mutex.
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
|
Hi, as you can see, your PR is blocked by rustbot. It has 2 issues:
Please check again and apply bot's suggestions. When all checks are green, add |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
how do i add the label, please |
|
We have a bot for labels (it also handles auto-assignments). You can use the shorthand |
This comment was marked as outdated.
This comment was marked as outdated.
I do need help with that please, because everything looks good from here |
|
You may follow the instructions offered by rustbot. Also, it will be better to squash these commits into one. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
|
should i fork again and create a new pr to fix these merge commits issues. im really having trouble on it |
|
@Psalmuel01 you don't need to, but that's totally fine thing to do if it's easier for you |
|
tbvh, i'm totally lost now and dont know what to do next. this has been pending for so long |
You could try: or to just update Then To rebase onto Basically, I never use |
|
Thanks @jieyouxu, will try this |
This comment was marked as resolved.
This comment was marked as resolved.
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
@Psalmuel01 |
|
@Psalmuel01 |
docs: Documented Send and Sync requirements for Mutex + MutexGuard This an attempt to continue where rust-lang#123225 left off. I did some light clean up from the work done in that PR. I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge. Let me know if I got anything wrong 😄 fixes rust-lang#122856 cc: `@IoaNNUwU` r? `@joboet`
docs: Documented Send and Sync requirements for Mutex + MutexGuard This an attempt to continue where rust-lang#123225 left off. I did some light clean up from the work done in that PR. I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge. Let me know if I got anything wrong 😄 fixes rust-lang#122856 cc: ``@IoaNNUwU`` r? ``@joboet``
Rollup merge of rust-lang#135684 - ranger-ross:mutex-docs, r=joboet docs: Documented Send and Sync requirements for Mutex + MutexGuard This an attempt to continue where rust-lang#123225 left off. I did some light clean up from the work done in that PR. I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge. Let me know if I got anything wrong 😄 fixes rust-lang#122856 cc: ``@IoaNNUwU`` r? ``@joboet``
docs: Documented Send and Sync requirements for Mutex + MutexGuard This an attempt to continue where rust-lang#123225 left off. I did some light clean up from the work done in that PR. I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge. Let me know if I got anything wrong 😄 fixes rust-lang#122856 cc: ``@IoaNNUwU`` r? ``@joboet``
closes #122856
This is an update to the mutex docs for send and sync to fix the issue raised