Go Language Goroutines: Difference between revisions
Line 26: | Line 26: | ||
<font color=darkkhaki>what happens with the result of the function?</font> | <font color=darkkhaki>what happens with the result of the function?</font> | ||
If the invocation is done from <code>main()</code>, the scheduler always seems to continue to execute <code>main()</code>, it does not preempt <code>main()</code> to executes the new goroutine. Also, given the fact that when <code>main()</code> exists, [[#Main_Exit|all other goroutines are forcibly terminated]], unless there's a mechanism that ensures they will be executed, they might not be executed at all. It is bad practice to use <code>[[Go_Package_time#time.Sleep.28.29|time.Sleep()]]</code> to preempt and delay <code>main()</code>, because we're making assumptions about timing, | If the invocation is done from <code>main()</code>, the scheduler always seems to continue to execute <code>main()</code>, it does not preempt <code>main()</code> to executes the new goroutine. Also, given the fact that when <code>main()</code> exists, [[#Main_Exit|all other goroutines are forcibly terminated]], unless there's a mechanism that ensures they will be executed, they might not be executed at all. It is bad practice to use <code>[[Go_Package_time#time.Sleep.28.29|time.Sleep()]]</code> to preempt and delay <code>main()</code>, because we're making assumptions about timing, and these assumptions can be wrong. | ||
=Exiting= | =Exiting= |
Revision as of 02:37, 1 September 2023
Internal
Overview
A goroutine is a Go thread. Many goroutines execute within a single O/S thread, the main thread?. From the O/S point of view, only one thread is scheduled. The goroutine schedule is done by the Go runtime scheduler. The Go runtime scheduler uses a logical processor. The goroutines scheduled on a logical processor are executing concurrently, not |in parallel. However, it is possible to have more than one logical processor, each logical processors can be mapped onto an O/S thread, which may be scheduled to work on different cores. In this case, things may become parallel.
A goroutine is always created automatically, to run the main()
function.
Creation and Invocation
To explicitly create a goroutine and schedule it, use the go
keyword, by providing a function invocation.
func somefunc(i int) {
...
}
...
go somefunc(10)
Any function invocation can be used: go fmt.Printf("something")
.
Note that this syntax only schedules a goroutine. It is not determined when it will be actually executed.
what happens with the result of the function?
If the invocation is done from main()
, the scheduler always seems to continue to execute main()
, it does not preempt main()
to executes the new goroutine. Also, given the fact that when main()
exists, all other goroutines are forcibly terminated, unless there's a mechanism that ensures they will be executed, they might not be executed at all. It is bad practice to use time.Sleep()
to preempt and delay main()
, because we're making assumptions about timing, and these assumptions can be wrong.
Exiting
A goroutine exits when the code is complete.
When the main
goroutine is complete, all other goroutines are forced to exit. It is said that those goroutines exit early.