在这种情况下WaitGroup.Wait()意味着内存障碍吗? [英] Does WaitGroup.Wait() imply memory barrier in this case?

查看:442
本文介绍了在这种情况下WaitGroup.Wait()意味着内存障碍吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var condition bool 
var wg sync.WaitGroup
for _,item:= range items {
wg.Add(1)$ b $ (item){
if meetCondition(item){
condition = true
}
wg.Done()
}(item)
}
wg.Wait()
//在这里检查条件是否安全?

在旧论坛这里有一个关于这个问题的讨论:
https://groups.google.com/forum/#!topic/golang-nuts / 5oHzhzXCcmM 答案是肯定的,这是安全的。然后讨论离题使用原子等,这不是我想问的。



在规范中甚至没有提及WaitGroup,它是文档说WaitGroup.Wait:等待块,直到WaitGroup计数器为零。
没有设置任何发生 - 之前的关系(或者是否?)。

这是否意味着第一个回答在wg.Wait返回后检查条件是否安全是非官方的?如果它是官方的,我失踪的原因是什么?感谢您的回答。



更新:
这是@ peterSO's @ ravi的答案后更新。谢谢。



好吧,如果您选择的项目数> 1,显然可能会出现竞争状态。提示:条件只能设置为true。不过,我也有同样的问题。



或许我应该说:


  1. 底层架构可以仅限于x86,x64或ARM

  2. 项目数量可以是1


    更新2
    我在这里创建了后续问题,当项目数量== 1时:
    你能否让这个'不正确同步'的测试失败? 解决方案

是。在 wg.Wait() wg.Done()之间存在关系。这个简单的事实是出于任何原因未在文档中提及或去MM #7948 。感谢Ian Lance Taylor澄清它 golang-nuts / 5oHzhzXCcmM 。关于竞争条件,你可以在同一个线程中阅读更多。



有点令人失望的是,在一种自称并发的语言中,人们必须依赖一个好词来做基本的东西(右)。

var condition bool
var wg sync.WaitGroup
for _, item := range items {
    wg.Add(1)
    go func(item) {
        if meetsCondition(item) {
            condition = true
        }
        wg.Done()
    }(item)
}
wg.Wait()
// is it safe to check condition here?

There is a discussion of this question at the old go forum here: https://groups.google.com/forum/#!topic/golang-nuts/5oHzhzXCcmM The answer there was yes, it is safe. Then the discussion digress to use of atomic, etc., which is not what I want to ask about.

There is not even one mention of WaitGroup in the spec and it's documentation is saying WaitGroup.Wait: "Wait blocks until the WaitGroup counter is zero." which does not set any happens-before relationship (or does it?).

Does it mean that the first answer "It is safe to check condition after wg.Wait returns" is unofficial? If it's official what are the reasons for it I am missing? Thanks alot if you answer.

Update: This is update after @peterSO's @ravi's answers. Thanks.

Well, obviously there can be a race condition if you choose number of items > 1. Hint: condition can be set only to true. Still, I have the same question.

And probably, I should have stated that:

  1. underlying architectures can be only x86, x64 or ARM
  2. number of items can be 1

Update 2 I created followup question for the case when number of items == 1 here: Can you make this 'incorrectly synchronized' test fail?

解决方案

Yes. There is happens-before relationship between wg.Wait() and wg.Done(). This simple fact is for whatever reason not mentioned in doc or go MM #7948. Thanks Ian Lance Taylor for clarifying it golang-nuts/5oHzhzXCcmM. About the race condition you can read more in the same thread.

A bit disappointing that in a language that calls itself concurrent one has to rely on a 'good word' to do the basic stuff (right).

这篇关于在这种情况下WaitGroup.Wait()意味着内存障碍吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆