golang concurrency with real life examples

polaris · · 519 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Goroutine works like the thread in java or any other multi-threading programming language, though the internal concept is different. Goroutine is more lightweight than processor thread. It takes little memory (on average 4~8 KB) than regular thread. Goroutines are managed by thread manager in runtime.</p> <p><a href="https://medium.com/@thedevsaddam/go-102-golang-concurrency-by-go-routine-3e0eab1a8ecb" rel="nofollow">see full article</a> </p> <hr/>**评论:**<br/><br/>TheBeasSneeze: <pre><p>Can you move the waitgroup creation into the main function and then pass it to foo and bar? Bad practise making it global.</p></pre>thedevsaddam: <pre><p>Yes, it&#39;s bad using it globally. but for this simple example it&#39;s probably okay.</p></pre>slowratatoskr: <pre><p>this is like beating a dead horse.</p></pre>qu33ksilver: <pre><p>I am guessing you probably don&#39;t know OP. You don&#39;t know his programming background, how much of Go has he written. </p> <p>He has gone to the effort of writing something and share it with the community. Yes, the topic is goroutines which everyone here is already well familiar with. But from the point of view to someone who is learning Go for the first time, the content may be helpful.</p> <p>You may not have posted the comment in a flippant way, but as members of the community, I think we should not discourage people from writing posts. Be it of any technical depth.</p></pre>slowratatoskr: <pre><p>you&#39;re right. my bad. i just want to see more of the advance stuff that other people are doing with go.</p></pre>thedevsaddam: <pre><p>could you please give an example of feeding an alive horse?? for concurrency? </p></pre>jerf: <pre><pre><code>func (h *Horses) Feed(f FoodProvider) { for _, horse := h.Horses() { food := f.GetFood() go func() { horse.Feed(food) } } } </code></pre> <p>Although I must warn you that I have not tested this code, and it is distinctly possible it may contain serious bugs that, say, may force feed the last horse in the herd with all the food while starving all the other ones. As a random example that I picked out of nowhere.</p></pre>jamesrr39: <pre><p>that&#39;s because by the time the go routine is run, the horses loop has reached the last horse. If you have goroutines running in a loop, any variables that change should be passed into the goroutine, like:</p> <pre><code>go func(horse *Horse) { horse.Feed(food) }(horse) </code></pre> <p>Try that, and you will see each horse is fed (in a pretty random order).</p> <p>edit: playground link: <a href="https://play.golang.org/p/XgIpD5aFgH" rel="nofollow">https://play.golang.org/p/XgIpD5aFgH</a></p></pre>

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

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