-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve docs on Arc<T> and Send/Sync #41536
Conversation
r? @BurntSushi (rust_highfive has picked a reviewer for you, use r? to override) |
src/liballoc/arc.rs
Outdated
/// `Arc<T>` will implement [`Send`] and [`Sync`] as long as the `T` implements | ||
/// [`Send`] and [`Sync`]. This may be a bit counter-intuitive at first: after | ||
/// all, isn't the point of `Arc<T>` thread safety? The key is this: `Arc<T>` | ||
/// is an implementation of multiple ownership that is thread safe, but it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe state here, that Arc<T>
is threadsafe in the way that you can safely clone or drop Arc<T>
s pointing to the same data in multiple threads at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you construct this if you cannot send an arc to a different thread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you had a type T
that is !Sync
and an Arc<T>
that does not give you access to the internal object, then the Arc<T>
can be Send
, because you can never access the inner data, but you can clone and drop the Arc<T>
to your heart's desire. This is the guarantee that Arc<T>
gives, without looking at the T
, you can clone and drop the Arc<T>
. Since Arc<T>
also gives you access to it's inner data through Deref
, you also need the Sync
bound on T
to make Arc<T>
Send
, but that's nothing special about Arc<T>
but a general thing about sharing data over thread boundaries as stated in the Sync
docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but impl<T> Send for Arc<T> where T: Send + Sync + ?Sized
, that is, if T
is !Sync
, the Arc<T>
will not be Send
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that. I just meant to explain the reasoning behind that a bit in different words than "is an implementation of multiple ownership that is thread safe", because i totally stumbled over that sentence and would have not understood it at all 5 years ago
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah okay. I wish I had a way to re-word it that didn't involve something that's not true....... hm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like
Arc<T>
makes it thread safe to create multiple pointers to the same data, but it ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tweaked this slightly, let me know what you think.
src/liballoc/arc.rs
Outdated
/// more flexibility. | ||
/// | ||
/// `Arc<T>` will implement [`Send`] and [`Sync`] as long as the `T` implements | ||
/// [`Send`] and [`Sync`]. This may be a bit counter-intuitive at first: after | ||
/// all, isn't the point of `Arc<T>` thread safety? The key is this: `Arc<T>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find that saying "T: Send/Sync
⇔ Arc<T>: Send/Sync
is counter-intuitive" is counter-intuitive 😅. It may be clearer to add one more sentence explaining the counter-intuitiveness, e.g. "Why it is not able hide the non-thread-safe type T
to make itself Arc<T>
thread-safe?"
src/liballoc/arc.rs
Outdated
/// | ||
/// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference | ||
/// counting. This means that it is thread-safe. The disadvantage is that | ||
/// atomic operations are more expensive than ordinary memory accesses. If you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most tiniest of nits: What's with the double spaces after some sentences? 😄
tl;dr: a bad vim config, and me missing fixes in some spots. ha!
…On Wed, Apr 26, 2017 at 4:45 AM, Pascal Hertleif ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/liballoc/arc.rs
<#41536 (comment)>:
> @@ -54,16 +54,32 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
/// exception. If you need to mutate through an `Arc`, use [`Mutex`][mutex],
/// [`RwLock`][rwlock], or one of the [`Atomic`][atomic] types.
///
-/// `Arc` uses atomic operations for reference counting, so `Arc`s can be
-/// sent between threads. In other words, `Arc<T>` implements [`Send`]
-/// as long as `T` implements [`Send`] and [`Sync`][sync]. The disadvantage is
-/// that atomic operations are more expensive than ordinary memory accesses.
-/// If you are not sharing reference-counted values between threads, consider
-/// using [`rc::Rc`][`Rc`] for lower overhead. [`Rc`] is a safe default, because
-/// the compiler will catch any attempt to send an [`Rc`] between threads.
-/// However, a library might choose `Arc` in order to give library consumers
+/// ## Thread Safety
+///
+/// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
+/// counting. This means that it is thread-safe. The disadvantage is that
+/// atomic operations are more expensive than ordinary memory accesses. If you
The most tiniest of nits: What's with the double spaces after *some*
sentences? 😄
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#41536 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABsivgM8i7Yyeu8w72_Vei1fBZm4CW7ks5rzwQOgaJpZM4NHp0M>
.
|
I plan on updating this PR early next week; sorry about the delay. |
src/liballoc/arc.rs
Outdated
/// `RefCell<T>` isn't [`Send`], and if `Arc<T>` was always [`Send`], | ||
/// `Arc<RefCell<T>>` would be as well. But then we'd have a problem: | ||
/// `RefCell<T>` is not thread safe; it keeps track of the borrowing count | ||
/// using non-atomic operations. It works the same way with [`Sync`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RefCell<T>
is Send
when T
is Send
. :-) I think you could either swap RefCell<T>
for Rc<T>
and change the "borrowing count" to "reference count," or you could keep RefCell<T>
and change Send
to Sync
(since RefCell<T>
is never Sync
) and then drop the last sentence.
src/liballoc/arc.rs
Outdated
/// using non-atomic operations. It works the same way with [`Sync`]. | ||
/// | ||
/// In the end, this means that you may need to pair `Arc<T>` with some sort of | ||
/// `std::sync` type, usually `Mutex<T>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is an important sentence, and it probably flows better if you keep RefCell
in the preceding paragraph.
I like this @steveklabnik. :-) r=me once feedback is addressed. |
This is something I always forget, so let's actually explain in the docs.
I believe I've fixed up everything, but I'd like to hear what you all have to say before I use @BurntSushi 's |
LGTM 👍. There is still an instance of double-space but I don't think it is a big issue. |
LGTM. |
@bors: r=burntsushi rollup |
📌 Commit 2f6744c has been approved by |
…ushi Improve docs on Arc<T> and Send/Sync This is something I always forget, so let's actually explain in the docs. I didn't fully link up everything here, but I'd like to make sure that the wording is okay before I bother.
…ushi Improve docs on Arc<T> and Send/Sync This is something I always forget, so let's actually explain in the docs. I didn't fully link up everything here, but I'd like to make sure that the wording is okay before I bother.
…ushi Improve docs on Arc<T> and Send/Sync This is something I always forget, so let's actually explain in the docs. I didn't fully link up everything here, but I'd like to make sure that the wording is okay before I bother.
This is something I always forget, so let's actually
explain in the docs.
I didn't fully link up everything here, but I'd like to make sure that the wording is okay before I bother.