Skip to content
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

Merged
merged 1 commit into from
May 10, 2017
Merged

Conversation

steveklabnik
Copy link
Member

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.

@rust-highfive
Copy link
Collaborator

r? @BurntSushi

(rust_highfive has picked a reviewer for you, use r? to override)

/// `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
Copy link
Contributor

@oli-obk oli-obk Apr 25, 2017

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Contributor

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 ...

Copy link
Member Author

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.

/// 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>`
Copy link
Member

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/SyncArc<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?"

///
/// 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
Copy link
Member

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? 😄

@steveklabnik
Copy link
Member Author

steveklabnik commented Apr 26, 2017 via email

@aidanhs aidanhs added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 26, 2017
@steveklabnik
Copy link
Member Author

I plan on updating this PR early next week; sorry about the delay.

/// `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`].
Copy link
Member

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.

/// 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>`.
Copy link
Member

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.

@BurntSushi
Copy link
Member

I like this @steveklabnik. :-) r=me once feedback is addressed.

This is something I always forget, so let's actually
explain in the docs.
@steveklabnik
Copy link
Member Author

I believe I've fixed up everything, but I'd like to hear what you all have to say before I use @BurntSushi 's r=me 😄

@kennytm
Copy link
Member

kennytm commented May 9, 2017

LGTM 👍. There is still an instance of double-space but I don't think it is a big issue.

@BurntSushi
Copy link
Member

LGTM.

@steveklabnik
Copy link
Member Author

@bors: r=burntsushi rollup

@bors
Copy link
Contributor

bors commented May 9, 2017

📌 Commit 2f6744c has been approved by burntsushi

frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 10, 2017
…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.
frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 10, 2017
…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.
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 10, 2017
…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.
bors added a commit that referenced this pull request May 10, 2017
Rollup of 5 pull requests

- Successful merges: #41531, #41536, #41809, #41854, #41886
- Failed merges:
@bors bors merged commit 2f6744c into rust-lang:master May 10, 2017
@steveklabnik steveklabnik deleted the arc-and-send branch October 25, 2017 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants