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