go - What is the execution time point of goroutine? -
i run following go code 10 times:
package main import ( "fmt" "time" ) func main() { go fmt.println("hello") fmt.println("world") time.sleep(1 * time.millisecond) }
the output "world
" first:
world hello
does manifest goroutine
execute unitl there block in main
routine? execution time point of goroutine
?
what execution time point of goroutine?
the compiler insert yield points program @ different locations seems adequate. example, in function calls , seemingly tight loops. also, goroutine yield when blocking on syscall. program probably yield execution on second goroutine when first goroutine reaches fmt.println("world")
, enters write syscall. after non-determinism ensues because don't know how long syscall take.
this, however, implementation detail , should not concerned it. when talking parallelism, timing guarantees (i.e. "happens before") have provided concurrency primitives standard library's sync/atomic
package.
tl;dr: don't rely on yield points program work correctly.
Comments
Post a Comment