{"action":"created","comment":{"url":"https://api.github.com/repos/manastech/crystal/pulls/comments/52772753","id":52772753,"diff_hunk":"@@ -0,0 +1,48 @@\n+# Concurrency\n+\n+To execute a block of code without blocking the main routine, use `spawn`:\n+\n+```crystal\n+spawn do\n+  puts \"Hello!\"\n+end\n+```\n+\n+The above program prints nothing because the program exited before the threaded code could finish.\n+This is fine if you are implementing a sort of \"fire and forget\" operation.\n+\n+To receive the output of a threaded operation, implement a `Channel`:\n+\n+```crystal\n+channel = Channel(String).new\n+spawn do\n+  channel.send \"Hello!\"\n+end\n+puts channel.receive\n+```\n+\n+Now you will see \"Hello!\" printed.\n+\n+Channels are a threaded \"conduit\" through which you can send and receive values.\n+Both `send` and `receive` are synchronous operations that block the current routine but not other routines.\n+Because of this behavior the above example implements `send` inside a `spawn` block.\n+\n+`receive` will block the current routine until the number of `send`s match the number of `receive`s.\n+This allows you to send asyncrhonous operations down a channel and wait for all operations to complete before continuing.","path":"_gitbook/syntax_and_semantics/concurrency.md","position":31,"original_position":31,"commit_id":"8ab8705bee4a594464525ca6e5f13ea693dcb880","original_commit_id":"8ab8705bee4a594464525ca6e5f13ea693dcb880","user":{"login":"arktisklada","id":1591025,"avatar_url":"https://avatars.githubusercontent.com/u/1591025?v=3","gravatar_id":"","url":"https://api.github.com/users/arktisklada","html_url":"https://github.com/arktisklada","followers_url":"https://api.github.com/users/arktisklada/followers","following_url":"https://api.github.com/users/arktisklada/following{/other_user}","gists_url":"https://api.github.com/users/arktisklada/gists{/gist_id}","starred_url":"https://api.github.com/users/arktisklada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arktisklada/subscriptions","organizations_url":"https://api.github.com/users/arktisklada/orgs","repos_url":"https://api.github.com/users/arktisklada/repos","events_url":"https://api.github.com/users/arktisklada/events{/privacy}","received_events_url":"https://api.github.com/users/arktisklada/received_events","type":"User","site_admin":false},"body":"Good eye!","created_at":"2016-02-12T17:39:39Z","updated_at":"2016-02-12T17:39:39Z","html_url":"https://github.com/manastech/crystal/pull/2113#discussion_r52772753","pull_request_url":"https://api.github.com/repos/manastech/crystal/pulls/2113","_links":{"self":{"href":"https://api.github.com/repos/manastech/crystal/pulls/comments/52772753"},"html":{"href":"https://github.com/manastech/crystal/pull/2113#discussion_r52772753"},"pull_request":{"href":"https://api.github.com/repos/manastech/crystal/pulls/2113"}}},"pull_request":{"url":"https://api.github.com/repos/manastech/crystal/pulls/2113","id":58550990,"html_url":"https://github.com/manastech/crystal/pull/2113","diff_url":"https://github.com/manastech/crystal/pull/2113.diff","patch_url":"https://github.com/manastech/crystal/pull/2113.patch","issue_url":"https://api.github.com/repos/manastech/crystal/issues/2113","number":2113,"state":"open","locked":false,"title":"Explaining concurrency","user":{"login":"arktisklada","id":1591025,"avatar_url":"https://avatars.githubusercontent.com/u/1591025?v=3","gravatar_id":"","url":"https://api.github.com/users/arktisklada","html_url":"https://github.com/arktisklada","followers_url":"https://api.github.com/users/arktisklada/followers","following_url":"https://api.github.com/users/arktisklada/following{/other_user}","gists_url":"https://api.github.com/users/arktisklada/gists{/gist_id}","starred_url":"https://api.github.com/users/arktisklada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arktisklada/subscriptions","organizations_url":"https://api.github.com/users/arktisklada/orgs","repos_url":"https://api.github.com/users/arktisklada/repos","events_url":"https://api.github.com/users/arktisklada/events{/privacy}","received_events_url":"https://api.github.com/users/arktisklada/received_events","type":"User","site_admin":false},"body":"There is a huge gap in documentation around concurrency.  This is just a first draft at attempting to explain `spawn` and the basic use of channels.  Let me know what you think","created_at":"2016-02-07T17:05:27Z","updated_at":"2016-02-12T17:39:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f1ba52370b25fe6a730aaf630ee16e59e095ad4d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/manastech/crystal/pulls/2113/commits","review_comments_url":"https://api.github.com/repos/manastech/crystal/pulls/2113/comments","review_comment_url":"https://api.github.com/repos/manastech/crystal/pulls/comments{/number}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/2113/comments","statuses_url":"https://api.github.com/repos/manastech/crystal/statuses/8ab8705bee4a594464525ca6e5f13ea693dcb880","head":{"label":"arktisklada:explaining-concurrency","ref":"explaining-concurrency","sha":"8ab8705bee4a594464525ca6e5f13ea693dcb880","user":{"login":"arktisklada","id":1591025,"avatar_url":"https://avatars.githubusercontent.com/u/1591025?v=3","gravatar_id":"","url":"https://api.github.com/users/arktisklada","html_url":"https://github.com/arktisklada","followers_url":"https://api.github.com/users/arktisklada/followers","following_url":"https://api.github.com/users/arktisklada/following{/other_user}","gists_url":"https://api.github.com/users/arktisklada/gists{/gist_id}","starred_url":"https://api.github.com/users/arktisklada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arktisklada/subscriptions","organizations_url":"https://api.github.com/users/arktisklada/orgs","repos_url":"https://api.github.com/users/arktisklada/repos","events_url":"https://api.github.com/users/arktisklada/events{/privacy}","received_events_url":"https://api.github.com/users/arktisklada/received_events","type":"User","site_admin":false},"repo":{"id":48973755,"name":"crystal","full_name":"arktisklada/crystal","owner":{"login":"arktisklada","id":1591025,"avatar_url":"https://avatars.githubusercontent.com/u/1591025?v=3","gravatar_id":"","url":"https://api.github.com/users/arktisklada","html_url":"https://github.com/arktisklada","followers_url":"https://api.github.com/users/arktisklada/followers","following_url":"https://api.github.com/users/arktisklada/following{/other_user}","gists_url":"https://api.github.com/users/arktisklada/gists{/gist_id}","starred_url":"https://api.github.com/users/arktisklada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arktisklada/subscriptions","organizations_url":"https://api.github.com/users/arktisklada/orgs","repos_url":"https://api.github.com/users/arktisklada/repos","events_url":"https://api.github.com/users/arktisklada/events{/privacy}","received_events_url":"https://api.github.com/users/arktisklada/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/arktisklada/crystal","description":"The Crystal Programming Language","fork":true,"url":"https://api.github.com/repos/arktisklada/crystal","forks_url":"https://api.github.com/repos/arktisklada/crystal/forks","keys_url":"https://api.github.com/repos/arktisklada/crystal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/arktisklada/crystal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/arktisklada/crystal/teams","hooks_url":"https://api.github.com/repos/arktisklada/crystal/hooks","issue_events_url":"https://api.github.com/repos/arktisklada/crystal/issues/events{/number}","events_url":"https://api.github.com/repos/arktisklada/crystal/events","assignees_url":"https://api.github.com/repos/arktisklada/crystal/assignees{/user}","branches_url":"https://api.github.com/repos/arktisklada/crystal/branches{/branch}","tags_url":"https://api.github.com/repos/arktisklada/crystal/tags","blobs_url":"https://api.github.com/repos/arktisklada/crystal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/arktisklada/crystal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/arktisklada/crystal/git/refs{/sha}","trees_url":"https://api.github.com/repos/arktisklada/crystal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/arktisklada/crystal/statuses/{sha}","languages_url":"https://api.github.com/repos/arktisklada/crystal/languages","stargazers_url":"https://api.github.com/repos/arktisklada/crystal/stargazers","contributors_url":"https://api.github.com/repos/arktisklada/crystal/contributors","subscribers_url":"https://api.github.com/repos/arktisklada/crystal/subscribers","subscription_url":"https://api.github.com/repos/arktisklada/crystal/subscription","commits_url":"https://api.github.com/repos/arktisklada/crystal/commits{/sha}","git_commits_url":"https://api.github.com/repos/arktisklada/crystal/git/commits{/sha}","comments_url":"https://api.github.com/repos/arktisklada/crystal/comments{/number}","issue_comment_url":"https://api.github.com/repos/arktisklada/crystal/issues/comments{/number}","contents_url":"https://api.github.com/repos/arktisklada/crystal/contents/{+path}","compare_url":"https://api.github.com/repos/arktisklada/crystal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/arktisklada/crystal/merges","archive_url":"https://api.github.com/repos/arktisklada/crystal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/arktisklada/crystal/downloads","issues_url":"https://api.github.com/repos/arktisklada/crystal/issues{/number}","pulls_url":"https://api.github.com/repos/arktisklada/crystal/pulls{/number}","milestones_url":"https://api.github.com/repos/arktisklada/crystal/milestones{/number}","notifications_url":"https://api.github.com/repos/arktisklada/crystal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/arktisklada/crystal/labels{/name}","releases_url":"https://api.github.com/repos/arktisklada/crystal/releases{/id}","deployments_url":"https://api.github.com/repos/arktisklada/crystal/deployments","created_at":"2016-01-04T03:58:06Z","updated_at":"2016-01-15T18:32:03Z","pushed_at":"2016-02-09T23:51:41Z","git_url":"git://github.com/arktisklada/crystal.git","ssh_url":"git@github.com:arktisklada/crystal.git","clone_url":"https://github.com/arktisklada/crystal.git","svn_url":"https://github.com/arktisklada/crystal","homepage":"http://crystal-lang.org","size":39756,"stargazers_count":0,"watchers_count":0,"language":"Crystal","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"manastech:gh-pages","ref":"gh-pages","sha":"b049eb341ebe44a6e36b10641ccf6babf4315105","user":{"login":"manastech","id":1354950,"avatar_url":"https://avatars.githubusercontent.com/u/1354950?v=3","gravatar_id":"","url":"https://api.github.com/users/manastech","html_url":"https://github.com/manastech","followers_url":"https://api.github.com/users/manastech/followers","following_url":"https://api.github.com/users/manastech/following{/other_user}","gists_url":"https://api.github.com/users/manastech/gists{/gist_id}","starred_url":"https://api.github.com/users/manastech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manastech/subscriptions","organizations_url":"https://api.github.com/users/manastech/orgs","repos_url":"https://api.github.com/users/manastech/repos","events_url":"https://api.github.com/users/manastech/events{/privacy}","received_events_url":"https://api.github.com/users/manastech/received_events","type":"Organization","site_admin":false},"repo":{"id":6887813,"name":"crystal","full_name":"manastech/crystal","owner":{"login":"manastech","id":1354950,"avatar_url":"https://avatars.githubusercontent.com/u/1354950?v=3","gravatar_id":"","url":"https://api.github.com/users/manastech","html_url":"https://github.com/manastech","followers_url":"https://api.github.com/users/manastech/followers","following_url":"https://api.github.com/users/manastech/following{/other_user}","gists_url":"https://api.github.com/users/manastech/gists{/gist_id}","starred_url":"https://api.github.com/users/manastech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manastech/subscriptions","organizations_url":"https://api.github.com/users/manastech/orgs","repos_url":"https://api.github.com/users/manastech/repos","events_url":"https://api.github.com/users/manastech/events{/privacy}","received_events_url":"https://api.github.com/users/manastech/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/manastech/crystal","description":"The Crystal Programming Language","fork":false,"url":"https://api.github.com/repos/manastech/crystal","forks_url":"https://api.github.com/repos/manastech/crystal/forks","keys_url":"https://api.github.com/repos/manastech/crystal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/manastech/crystal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/manastech/crystal/teams","hooks_url":"https://api.github.com/repos/manastech/crystal/hooks","issue_events_url":"https://api.github.com/repos/manastech/crystal/issues/events{/number}","events_url":"https://api.github.com/repos/manastech/crystal/events","assignees_url":"https://api.github.com/repos/manastech/crystal/assignees{/user}","branches_url":"https://api.github.com/repos/manastech/crystal/branches{/branch}","tags_url":"https://api.github.com/repos/manastech/crystal/tags","blobs_url":"https://api.github.com/repos/manastech/crystal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/manastech/crystal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/manastech/crystal/git/refs{/sha}","trees_url":"https://api.github.com/repos/manastech/crystal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/manastech/crystal/statuses/{sha}","languages_url":"https://api.github.com/repos/manastech/crystal/languages","stargazers_url":"https://api.github.com/repos/manastech/crystal/stargazers","contributors_url":"https://api.github.com/repos/manastech/crystal/contributors","subscribers_url":"https://api.github.com/repos/manastech/crystal/subscribers","subscription_url":"https://api.github.com/repos/manastech/crystal/subscription","commits_url":"https://api.github.com/repos/manastech/crystal/commits{/sha}","git_commits_url":"https://api.github.com/repos/manastech/crystal/git/commits{/sha}","comments_url":"https://api.github.com/repos/manastech/crystal/comments{/number}","issue_comment_url":"https://api.github.com/repos/manastech/crystal/issues/comments{/number}","contents_url":"https://api.github.com/repos/manastech/crystal/contents/{+path}","compare_url":"https://api.github.com/repos/manastech/crystal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/manastech/crystal/merges","archive_url":"https://api.github.com/repos/manastech/crystal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/manastech/crystal/downloads","issues_url":"https://api.github.com/repos/manastech/crystal/issues{/number}","pulls_url":"https://api.github.com/repos/manastech/crystal/pulls{/number}","milestones_url":"https://api.github.com/repos/manastech/crystal/milestones{/number}","notifications_url":"https://api.github.com/repos/manastech/crystal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/manastech/crystal/labels{/name}","releases_url":"https://api.github.com/repos/manastech/crystal/releases{/id}","deployments_url":"https://api.github.com/repos/manastech/crystal/deployments","created_at":"2012-11-27T17:32:32Z","updated_at":"2016-02-12T17:02:31Z","pushed_at":"2016-02-12T17:21:34Z","git_url":"git://github.com/manastech/crystal.git","ssh_url":"git@github.com:manastech/crystal.git","clone_url":"https://github.com/manastech/crystal.git","svn_url":"https://github.com/manastech/crystal","homepage":"http://crystal-lang.org","size":40927,"stargazers_count":4303,"watchers_count":4303,"language":"Crystal","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":353,"mirror_url":null,"open_issues_count":296,"forks":353,"open_issues":296,"watchers":4303,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/manastech/crystal/pulls/2113"},"html":{"href":"https://github.com/manastech/crystal/pull/2113"},"issue":{"href":"https://api.github.com/repos/manastech/crystal/issues/2113"},"comments":{"href":"https://api.github.com/repos/manastech/crystal/issues/2113/comments"},"review_comments":{"href":"https://api.github.com/repos/manastech/crystal/pulls/2113/comments"},"review_comment":{"href":"https://api.github.com/repos/manastech/crystal/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/manastech/crystal/pulls/2113/commits"},"statuses":{"href":"https://api.github.com/repos/manastech/crystal/statuses/8ab8705bee4a594464525ca6e5f13ea693dcb880"}}}}