Golang / GoLang Concurrency Mastery Interview Questions
What is asynchronous preemption in Go (1.14+) and why was it introduced?
Before Go 1.14, goroutine scheduling was cooperative: a goroutine only yielded its processor at specific safe points — function call sites, channel operations, and syscalls. A CPU-bound goroutine in a tight loop with no function calls could starve other goroutines indefinitely and block GC stop-the-world phases.
// Pre-1.14: this goroutine could starve all others on its P indefinitely
go func() {
for {
x := 0
for i := 0; i < 1_000_000_000; i++ { x++ }
// No function calls → no scheduling point
// Other goroutines on this P cannot run
// GC STW cannot proceed — GC pause stretches indefinitely
}
}()
// Go 1.14+: asynchronous preemption via SIGURG
// sysmon goroutine detects a goroutine running on a P for > 10ms
// It sends SIGURG to the OS thread running that goroutine
// The signal handler inserts a preemption point; the goroutine yields
// Effect:
// - Tight loops no longer starve other goroutines
// - GC STW completes in bounded time regardless of goroutine behaviour
// - Programs are more responsive under CPU-heavy workloads
// runtime.Gosched() — explicit cooperative yield (still useful)
for i := 0; i < 1_000_000; i++ {
doHeavyChunk(i)
if i%1000 == 0 {
runtime.Gosched() // voluntarily yield every 1000 iterations
}
}
// Preemption safety: goroutine stacks may be moved during preemption
// → All stack references must be valid Go pointers (enforces unsafe.Pointer rules)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
