Abstract
- User Thread managed by the Go runtime. This design decision allows goroutines to be lightweight(2kb) vs. megabytes for OS threads and efficiently multiplexed onto a smaller number of Kernel Thread
- The Go runtime scheduler handles the mapping of goroutines to kernel threads, utilizing techniques like multiplexing and asynchronous I/O to optimize performance
- Refer to here for more information
Go implements cooperative scheduling with preemption capabilities
Goroutines yield control at strategic points:
- Channel operations
- Network/blocking system calls
- Explicit yielding via
runtime.Gosched()
, read more herePreemption occurs during garbage collection or after time thresholds.
Advantages Over Other Languages
- First-class language feature (not a library), integrated with Go’s runtime systems
- Clean syntax with built-in primitives
Spinning up Goroutine
go <function_call>
- creates a User Thread managed by Go runtime- The program below simulates retrieving data from a Database. Each retrieval takes
500ms
. But the total execution time is around10µs
. Continue reading to find out why :). You can remove thego
keyword to see the performance boost goroutine brings
Program exits before other goroutines finish!
We need to introduce Synchronisation (同步) mechanism here to ensure the program only exists when all other goroutines finish running. Uncomment the code blocks in the editor above to add in the synchronization mechanism.
sync.WaitGroup
- a Synchronization mechanism used to coordinate the completion of multiple goroutines. It acts like a counter that multiple goroutines can interact with.
Add(int)
- increments the counter by a specified value, indicating the number of goroutines you will be waiting for
Done()
- decrements the counter. Used by each goroutine when it finishes
Wait()
- blocks the current goroutine until the counter reaches zero, signaling that all tracked goroutines have completed
Regarding the performance gainz
The above example shows a significant performance gainz - 5 iterations that take
500ms
each finish in500ms
! If you increase the number of iterations, it should still complete in about500ms
. Give it a try! This thanks to Concurrency (并发) and the lightweight nature of User Thread.However, this performance gainz are only for not CPU-bounded tasks like Async IO. The performance gainz declines gradually as the tasks require more CPU power to complete. For non CPU-bounded tasks, they can run in concurrently without needing attention from the CPU. In the code editor below, change the value of
i
in themain()
from100
to1000
, you should observe the time taken 10X!