-1

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

STEEL
  • 8,955
  • 9
  • 67
  • 89
  • 4
    What's the race condition? You want the print from the goroutine to display before the print from main? – mkopriva Mar 29 '19 at 10:32
  • 2
    I don't see a race condition. – Jonathan Hall Mar 29 '19 at 10:33
  • race condition I meant where `go sayHellow()` 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 – STEEL Mar 29 '19 at 10:54

1 Answers1

4

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).

Nik
  • 2,885
  • 2
  • 25
  • 25
  • thanks. Also can you suggest what would be the best code for the same ? I mean how do I fix it – STEEL Mar 29 '19 at 10:57
  • 1
    Could you extend your question with expected output? – Nik Mar 29 '19 at 10:59
  • 1
    Using WaitGroup is the best. See here a comparison of two solutions: WaitGroup vs channels https://stackoverflow.com/q/36056615/9811969 – Nik Mar 29 '19 at 12:15