Given the following pseudo-code:
func main() {
go runFuncOne()
}
func runFuncOne() bool {
runFuncTwo()
return true
}
func runFuncTwo() bool {
// Do some heavy work
return true
}
Would runFuncTwo
only be blocking to runFuncOne
(the calling goroutine) or would runFuncTwo
also block main()
as it is not itself running as a goroutine?
My assumption is that main()
will open a thread within which runFuncOne()
and runFuncTwo()
will then operate. Any work performed within runFuncTwo()
will then only block this instance of runFuncOne()
?
runFuncTwo blocks runFuncOne only, as both are running in a separate Go routine.
Note though that main() will therefore continue and exit, causing the program to exit. To avoid this and all runFuncTwo to complete, you should use a sync.WaitGroup.
The Go Programming Language Specification
The function value and parameters are evaluated as usual in the calling goroutine, but unlike with a regular call, program execution does not wait for the invoked function to complete. Instead, the function begins executing independently in a new goroutine. When the function terminates, its goroutine also terminates. If the function has any return values, they are discarded when the function completes.
func main() {
go runFuncOne()
}
main
will invoke runFuncOne()
as a goroutine and then exit the program immediately, terminating runFuncOne()
without waiting for it to complete.
func runFuncOne() bool {
runFuncTwo()
return true
}
func runFuncTwo() bool {
// Do some heavy work
return true
}
runFuncOne()
makes a function call to runFuncTwo()
so it will wait for runFuncTwo()
to complete.
Can also use a channel for synchronization:
package main
import (
"fmt"
"time"
)
func main() {
var ch chan int = make(chan int)
go runFuncOne(ch)
fmt.Println("Waiting..")
fmt.Println(<-ch)
}
func runFuncOne(ch chan int) {
runFuncTwo(ch)
ch <- 1
}
func runFuncTwo(ch chan int) bool {
time.Sleep(1 * time.Second)
fmt.Println("Done working..")
return true
}
https://play.golang.org/p/h1S4TSdT0w
Return type of runFuncOne
won't be of consequence if you are calling it with a go routine.