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

Added cancellation-token content in Graceful Shutdown topic docs #695

Merged
merged 4 commits into from
Apr 3, 2023

Conversation

stanwolverine
Copy link
Contributor

Cancellation Tokens should be used to notify task to shut down instead of broadcast channels. Therefore, I replaced usage of Broadcast Channel in Telling things to shut down section of Graceful Shutdown topic docs with Cancellation Tokens

closes #694

Cancellation Tokens should be used to notify task to shut down instead of broadcast channels

closes tokio-rs#694
@Darksonn
Copy link
Contributor

Generally, to share a token between multiple tasks, you need to clone it. It would be good to include a bit about that. Also, I don't think your example compiles due to this issue.

@stanwolverine
Copy link
Contributor Author

@Darksonn You are right. My example was not compiling due to ownership rule. I have included token cloning in doc and code snippet.

easy to implement graceful shutdowns.

Note that, a `CancellationToken` can only be used by one task at a time due to
ownership rule. To use the same `CancellationToken` in multiple tasks you can make
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grammar here is a bit awkward. How about this?

To share a cancellation token between several tasks, you must clone it. This is due to the single ownership rule that requires that each value has a single owner. When cloning a token, you get another value that's indistinguishable from the original; if one is cancelled, then the other is also cancelled. You can make as many clones as you need, and when you call cancel on one of them, they're all cancelled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a good explanation. I will add this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Darksonn I have updated the content

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@Darksonn Darksonn merged commit 8ad0da6 into tokio-rs:master Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tokio's tutorial does not mention CancellationToken
2 participants