Vicki Niu - Goroutines: Under the Hood

Goroutines: Under the Hood

Goroutines are a key part of Go’s powerful concurrency model, but what really happens when you kick off a goroutine? We’ll walk through what go func() really does, while noting some of the common pitfalls when working with goroutines.

About Vicki

Vicki is a software engineer at Byte based in New York City. When not writing Go code, you can find Vicki working on civic tech projects with the US Digital Response, mentoring future software engineers with Emergent Works, or watching reality TV.

Feel free to follow and connect with Vicki on Twitter, LinkedIn, GitHub!

Have questions for Vicki?

Please submit them in this thread. Vicki would love to answer them!

Haven’t signed up for the free conference yet?

Grab your free tickets here :slight_smile:

3 Likes

How do Goroutines achieve Parallelism ?

Hi Vicki,

Great talk!

I have two questions:

  1. Why does the runtime maintain logical notion of processors as opposed to actual processors?
  2. What is the execution context of scheduler? Does it run under a special privilege and not just another G in the runtime?

Thanks,
Karan

Go will spawn up to GOMAXPROCS threads. The Goroutines will be schedule across them.

By default GOMAXPROCS is set to the supported thread count of your CPU, so you can just make the goroutines, if the hardware allows for parallelism they will be parallelized, if it doesn’t they will be scheduled sequentially.

Great question, Syed! To only half-answer your question, I’d say that the focus of Goroutines is concurrency, not parallelism. I can’t recommend Rob Pike’s talk on the subject more highly!

Of course, goroutines may happen to execute in parallel if you are on a multi-core system, but the motivation for using goroutines when we write code is not to achieve parallel execution, but rather to write our programs in a concurrent way, where we can handle a great deal of things at the same time, even if we are not executing a lot of operations simultaneously (to badly paraphrase Rob Pike’s talk).

2 Likes

Hi Vicki!

Any chance the slide deck will be available for your talk today?

Thanks so much, Karan! and thanks for the excellent questions :slight_smile:

  1. I would say the reason we talk about P as a logical processor is because it exists only as a “logical” notion within the code of the Go runtime, and doesn’t necessarily have any relationship to a physical processor. There is usually a relationship between the logical P in the Go runtime and the physical processor on your machine, but the P struct is where the runtime keeps important scheduler-related data, such as what goroutines are in the queue.

  2. The scheduler is a part of the Go runtime! The goroutines interact with the runtime through the runtime package — so while we talk about “the scheduler” as if it is a separate entity running somewhere, it is one of a number of components of the Go runtime (including the Go garbage collector!). Go is a little different than, say Java, since the runtime is not a VM, but a library that implements these language features (see: Go FAQ).

Hopefully that helps to address your questions!

3 Likes

Just opened access to the slide deck here! I believe the amazing conference team will also be uploading video recordings of the talks also this week :slight_smile:

2 Likes

Thank you! Fantastic talk today :slight_smile:

1 Like

It will take us some days to upload the edited individual talks. In the meantime, please continue watching @vickiniu’s awesome talk using the live stream link. :slight_smile:

Thanks Vicky, that definitely was helpful!