3 ms·
How do goroutines map to native OS threads? How do they get scheduled? It all seems a bit magical to me - which is great when things work as expected...
by topbanana 13y ago
How do goroutines map to native OS threads? How do they get scheduled? It all seems a bit magical to me - which is great when things work as expected...
- lucian1900 13y agoThey get multiplexed across a few OS threads. There is a scheduler which will leave goroutines that do a native (potentially blocking) call in their own thread.
- brandonbloom 13y agoIs it really any more magical than how OS threads get mapped to CPU cores?
- georgemcbay 13y agoThe Go runtime has a built-in scheduler which historically would be called into action for possible preemption whenever an OS system call is made or could be invoked manually in Go code via runtime.Gosched(). Go 1.2 added a change allowing the scheduler to possibly be kicked off during any Go language function call which reduces the likelihood of being locked in a tight loop that never makes a system call. There are obviously still cases where you can trap yourself into a deadlock by tight looping in code that makes no function calls at all, but it is pretty easy to detect this and adjust for it.