- read

Goroutine Scheduler Revealed: You’ll Never See Goroutines the Same Way Again

Phuong Le (@func25) 100

Goroutine Scheduler Revealed: You’ll Never See Goroutines the Same Way Again

You might have heard of the Goroutine Scheduler before, but how well do we really know how it works? How does it pair goroutines with threads?

Phuong Le (@func25)
Level Up Coding
Published in
8 min read4 hours ago

--

Behind the Scenes of Goroutines (source: blog.devtrovert.com)

If you found these infographics helpful, please give it some 👏 to let me know. I’d also love to hear any feedback you have.

Concurrency Series

Don’t worry about understanding the image above right now, as we’re going to begin with the very basics.

Goroutines are distributed into threads, which Goroutine Scheduler handle behind the scene.From our previous talks, we know a few things about goroutines:

  • Goroutines in terms of raw execution speed, aren’t necessarily faster than threads since they need an actual thread to run on.
  • The real advantage of goroutines lies in areas like context switching, memory footprint, the cost of creation and teardown.

You might have heard of the Goroutine Scheduler before, but how well do we really know how it works? How does it pair goroutines with threads?

Now breaking down the scheduler’s operation one step at a time.

1. The Goroutine M:N Scheduler

The Go Team has truly simplified concurrency for us, just think about it: creating a goroutine is as easy as prefixing a function with the go keyword.

go doWork()

But behind this easy step, there’s a deeper system at work.

Right from the start, Go didn’t simply provide us with threads. Instead, there’s a helper in the middle, the Goroutine Scheduler which is a key part of the Go Runtime.