{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/book/pulls/comments/105441661","pull_request_review_id":26313806,"id":105441661,"diff_hunk":"@@ -0,0 +1,83 @@\n+## Extensible Concurrency with the `Sync` and `Send` Traits\n+\n+One interesting aspect of Rust's concurrency model is that the language knows\n+*very* little about concurrency. Almost everything we've been talking about so\n+far has been part of the standard library, not the language itself. Because we\n+don't need the language to provide everything we need to program in a\n+concurrent context, we're not limited to the concurrency options that the\n+standard library or language provide: we can write our own or use ones others\n+have written.\n+\n+We said *almost* everything wasn't in the language, so what is? There are two\n+traits, both in `std::marker`: `Sync` and `Send`.\n+\n+### `Send` for Indicating Ownership May Be Transferred to Another Thread\n+\n+The `Send` marker trait indicates that ownership of that type may be\n+transferred between threads. Almost every Rust type is `Send`, but there are\n+some exceptions. One type provided by the standard library that is not `Send`\n+is `Rc<T>`: if we clone an `Rc<T>` value and try to transfer ownership of the\n+clone to another thread, both threads might update the reference count at the\n+same time. As we mentioned in the previous section, `Rc<T>` is implemented for\n+use in single-threaded situations where you don't want to pay the performance\n+penalty of having a threadsafe reference count.\n+\n+Because `Rc<T>` is not marked `Send`, Rust's type system and trait bounds\n+ensure that we can never forget and accidentally send an `Rc<T>` value across\n+threads unsafely. We tried to do this in Listing 16-14, and we got an error\n+that said `the trait Send is not implemented for Rc<Mutex<i32>>`. When we\n+switched to `Arc<T>`, which is `Send`, the code compiled.\n+\n+Any type that is composed entirely of `Send` types is automatically marked as\n+`Send` as well. Almost all primitive types are `Send`, aside from raw pointers,\n+which we'll discuss in Chapter 19. Most standard library types are `Send`,\n+aside from `Rc<T>`.\n+\n+### `Sync` for Indicating Access from Multiple Threads is Safe\n+\n+The `Sync` marker trait indicates that a type is safe to have references to a\n+value from multiple threads. Another way to say this is for any type `T`, `T`\n+is `Sync` if `&T` (a reference to `T`) is `Send` so that the reference can be\n+sent safely to another thread. In a similar manner as `Send`, primitive types\n+are `Sync` and types composed entirely of types that are `Sync` are also `Sync`.\n+\n+`Rc<T>` is also not `Sync`, for the same reasons that it's not `Send`.\n+`RefCell<T>` (another smart pointer we talked about in Chapter 15) and the\n+family of related `Cell<T>` types are not `Sync`. The implementation of the\n+borrow checking at runtime that they do is not threadsafe. `Mutex<T>` is\n+`Sync`, and can be used to share access with multiple threads as we saw in the\n+previous section.\n+\n+### Implementing `Send` and `Sync` Manually is Unsafe\n+\n+Usually, we don't need to implement the `Send` and `Sync` traits, since types\n+that are made up of `Send` and `Sync` traits are automatically also `Send` and\n+`Sync`. Because they're marker traits, they don't even have any methods to\n+implement. They're just useful for enforcing concurrency-related invariants.\n+\n+Implementing the guarantees that these traits are markers for involves\n+implementing unsafe Rust code. We're going to be talking about `unsafe` in","path":"second-edition/src/ch16-04-extensible-concurrency-sync-and-send.md","position":59,"original_position":59,"commit_id":"038d91d5a5af1cef044ea85f3f660ceb9b0da932","original_commit_id":"038d91d5a5af1cef044ea85f3f660ceb9b0da932","user":{"login":"matthewjasper","id":20113453,"avatar_url":"https://avatars2.githubusercontent.com/u/20113453?v=3","gravatar_id":"","url":"https://api.github.com/users/matthewjasper","html_url":"https://github.com/matthewjasper","followers_url":"https://api.github.com/users/matthewjasper/followers","following_url":"https://api.github.com/users/matthewjasper/following{/other_user}","gists_url":"https://api.github.com/users/matthewjasper/gists{/gist_id}","starred_url":"https://api.github.com/users/matthewjasper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthewjasper/subscriptions","organizations_url":"https://api.github.com/users/matthewjasper/orgs","repos_url":"https://api.github.com/users/matthewjasper/repos","events_url":"https://api.github.com/users/matthewjasper/events{/privacy}","received_events_url":"https://api.github.com/users/matthewjasper/received_events","type":"User","site_admin":false},"body":"'using unsafe Rust [code].'","created_at":"2017-03-10T17:07:47Z","updated_at":"2017-03-10T17:48:28Z","html_url":"https://github.com/rust-lang/book/pull/467#discussion_r105441661","pull_request_url":"https://api.github.com/repos/rust-lang/book/pulls/467","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/book/pulls/comments/105441661"},"html":{"href":"https://github.com/rust-lang/book/pull/467#discussion_r105441661"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/book/pulls/467"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/book/pulls/467","id":107691175,"html_url":"https://github.com/rust-lang/book/pull/467","diff_url":"https://github.com/rust-lang/book/pull/467.diff","patch_url":"https://github.com/rust-lang/book/pull/467.patch","issue_url":"https://api.github.com/repos/rust-lang/book/issues/467","number":467,"state":"open","locked":false,"title":"Ch16: concurrency","user":{"login":"steveklabnik","id":27786,"avatar_url":"https://avatars1.githubusercontent.com/u/27786?v=3","gravatar_id":"","url":"https://api.github.com/users/steveklabnik","html_url":"https://github.com/steveklabnik","followers_url":"https://api.github.com/users/steveklabnik/followers","following_url":"https://api.github.com/users/steveklabnik/following{/other_user}","gists_url":"https://api.github.com/users/steveklabnik/gists{/gist_id}","starred_url":"https://api.github.com/users/steveklabnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/steveklabnik/subscriptions","organizations_url":"https://api.github.com/users/steveklabnik/orgs","repos_url":"https://api.github.com/users/steveklabnik/repos","events_url":"https://api.github.com/users/steveklabnik/events{/privacy}","received_events_url":"https://api.github.com/users/steveklabnik/received_events","type":"User","site_admin":false},"body":"Draft done!","created_at":"2017-02-23T19:08:41Z","updated_at":"2017-03-10T17:48:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8780e82ce973a8b244bb64351954a83d11844136","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/book/pulls/467/commits","review_comments_url":"https://api.github.com/repos/rust-lang/book/pulls/467/comments","review_comment_url":"https://api.github.com/repos/rust-lang/book/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/book/issues/467/comments","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/038d91d5a5af1cef044ea85f3f660ceb9b0da932","head":{"label":"rust-lang:ch16-concurrency","ref":"ch16-concurrency","sha":"038d91d5a5af1cef044ea85f3f660ceb9b0da932","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":47854924,"name":"book","full_name":"rust-lang/book","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/book","description":"The Rust Programming Language","fork":false,"url":"https://api.github.com/repos/rust-lang/book","forks_url":"https://api.github.com/repos/rust-lang/book/forks","keys_url":"https://api.github.com/repos/rust-lang/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/book/teams","hooks_url":"https://api.github.com/repos/rust-lang/book/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/book/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/book/events","assignees_url":"https://api.github.com/repos/rust-lang/book/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/book/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/book/tags","blobs_url":"https://api.github.com/repos/rust-lang/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/book/languages","stargazers_url":"https://api.github.com/repos/rust-lang/book/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/book/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/book/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/book/subscription","commits_url":"https://api.github.com/repos/rust-lang/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/book/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/book/merges","archive_url":"https://api.github.com/repos/rust-lang/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/book/downloads","issues_url":"https://api.github.com/repos/rust-lang/book/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/book/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/book/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/book/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/book/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/book/deployments","created_at":"2015-12-11T22:49:49Z","updated_at":"2017-03-10T12:24:18Z","pushed_at":"2017-03-10T04:07:14Z","git_url":"git://github.com/rust-lang/book.git","ssh_url":"git@github.com:rust-lang/book.git","clone_url":"https://github.com/rust-lang/book.git","svn_url":"https://github.com/rust-lang/book","homepage":"http://rust-lang.github.io/book/","size":13144,"stargazers_count":610,"watchers_count":610,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":157,"mirror_url":null,"open_issues_count":69,"forks":157,"open_issues":69,"watchers":610,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"be80e73f4636359355c01862d92c8a98d5075c6d","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":47854924,"name":"book","full_name":"rust-lang/book","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/book","description":"The Rust Programming Language","fork":false,"url":"https://api.github.com/repos/rust-lang/book","forks_url":"https://api.github.com/repos/rust-lang/book/forks","keys_url":"https://api.github.com/repos/rust-lang/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/book/teams","hooks_url":"https://api.github.com/repos/rust-lang/book/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/book/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/book/events","assignees_url":"https://api.github.com/repos/rust-lang/book/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/book/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/book/tags","blobs_url":"https://api.github.com/repos/rust-lang/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/book/languages","stargazers_url":"https://api.github.com/repos/rust-lang/book/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/book/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/book/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/book/subscription","commits_url":"https://api.github.com/repos/rust-lang/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/book/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/book/merges","archive_url":"https://api.github.com/repos/rust-lang/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/book/downloads","issues_url":"https://api.github.com/repos/rust-lang/book/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/book/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/book/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/book/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/book/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/book/deployments","created_at":"2015-12-11T22:49:49Z","updated_at":"2017-03-10T12:24:18Z","pushed_at":"2017-03-10T04:07:14Z","git_url":"git://github.com/rust-lang/book.git","ssh_url":"git@github.com:rust-lang/book.git","clone_url":"https://github.com/rust-lang/book.git","svn_url":"https://github.com/rust-lang/book","homepage":"http://rust-lang.github.io/book/","size":13144,"stargazers_count":610,"watchers_count":610,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":157,"mirror_url":null,"open_issues_count":69,"forks":157,"open_issues":69,"watchers":610,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/book/pulls/467"},"html":{"href":"https://github.com/rust-lang/book/pull/467"},"issue":{"href":"https://api.github.com/repos/rust-lang/book/issues/467"},"comments":{"href":"https://api.github.com/repos/rust-lang/book/issues/467/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/book/pulls/467/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/book/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/book/pulls/467/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/book/statuses/038d91d5a5af1cef044ea85f3f660ceb9b0da932"}}}}
{"actor":{"display_login":"matthewjasper"}}