How to check/make program concurrent?

xuanbao · · 474 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Learning Go for a class. Trying to understand concurrency right now.</p> <p>Is this <a href="http://pastebin.com/vkRNmjMX" rel="nofollow">example program</a> concurrent? What would it look like if it were/were not?</p> <p>Is it as simple as removing the &#34;go func&#34; in the 24th line to make it concurrent?</p> <hr/>**评论:**<br/><br/>djherbis: <pre><p>From a quick look, the example looks concurrent. The &#34;go&#34; keyword runs the following function concurrently in a goroutine.</p> <p><a href="https://golang.org/doc/effective_go.html#goroutines" rel="nofollow">https://golang.org/doc/effective_go.html#goroutines</a></p> <p>This link might be helpful as well: <a href="https://www.golang-book.com/books/intro/10" rel="nofollow">https://www.golang-book.com/books/intro/10</a></p></pre>aylobng: <pre><p>Hm, so how would I go about making this program non-concurrent? Deleting &#34;go func()&#34; and &#34;()&#34; isn&#39;t enough. </p></pre>djherbis: <pre><p>In Go, we often use channels to communicate between concurrently running goroutines. In this case there are two channels &#34;calc&#34; and &#34;result&#34;. The checkval function sends input on the &#34;calc&#34; channel to the goroutine running the &#34;convert&#34; function, where it receives the value computes the result and sends it back to the checkval function via the &#34;results&#34; channel. </p> <p>To make it not-concurrent, you should take the computation being done in convert [the section of code between the send and receive] and use this code [possibly in its own function] in checkval instead of sending a value on &#34;calc&#34; and receiving it on &#34;results&#34;.</p> <p>Hope that helps.</p> <ul> <li>hopefully I&#39;m not doing your homework for you, please read the links I posted above to get a more complete explanation of what goroutines and channels are.</li> </ul></pre>mc_hammerd: <pre><p>should be enough.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

474 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传