duanmo5724 2019-03-29 10:29
浏览 5
已采纳

常规比赛条件解决方案

I'm trying to understand how can I fix this race condition for below code.

sayHello := func() {
    fmt.Println("Hello from goroutine")
}

go sayHello()
time.Sleep(1)

fmt.Println("Hello, playground")

Expectation: I just want to know whats the best solution, should I use WaitGroup or is there any better solution ?

So I came up with below solution :

var wg sync.WaitGroup
//defer wg.Wait()
sayHello := func() {
    defer wg.Done()
    fmt.Println("Hello from goroutine")
}
wg.Add(1)

go sayHello()
wg.Wait()

fmt.Println("Hello, playground")

But its blocking the main goroutine until code is executed !

As well, if I use defer wg.Wait() the output is different ! https://play.golang.org/p/_xkLb7HvNF8

Race condition I meant where go sayHello() never even gets executed, cause the main func will finish executing before the goroutine even started. Hence it creates a race condition if I try to put a time.Sleep

  • 写回答

1条回答 默认 最新

  • douhong1703 2019-03-29 10:41
    关注

    There is no a race condition in your code.

    First question

    But its blocking the main goroutine until code is executed !

    You are using right after the sayHello call:

    wg.Wait()
    

    This blocks your code and waits until the goroutine will be executed. So, go sayHello() will print "Hello from goroutine" always before "Hello, playground".

    See documentation here:

    Wait blocks until the WaitGroup counter is zero.

    Second question

    As well, if I use defer wg.Wait() the output is different !

    Yes, in this case wg.Wait() will be executed before exit the main function. This means sayHello() will print "Hello from goroutine" before or after "Hello, playground" - this depends on Go scheduler

    See more about defer here

    A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns. Defer is commonly used to simplify functions that perform various clean-up actions.

    Update:

    Using WaitGroup is recommended in comparison to another solution using channels. You should use wg.Wait() at the right place to achieve the expected output (still not provided).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案