Implementation of goroutines

I have been reading up on concurrency in Go. In particular, I have been wondering about the statement from a presentation by Brad Fitzpatrick:
n, err := io.Copy(dst, src)

  • copies n bytes from dst to src
  • synchronous (blocks)
  • Go runtime deals with making blocking efficient
In virtually all languages I have worked with (Java, Scala, javascript, ...), a call that blocks the execution of the current function also blocks the current thread. The only exception is C# with its rather complex async/await feature implemented in the compiler.

Now in Go, if you make a call that blocks the current execution, you are (usually) only blocking the current goroutine, not a thread. The Go runtime multiplexes goroutines on fewer OS threads.

The question therefore is: What are the fundamental differences in the implementation of goroutines compared to OS threads that make goroutines so much more lightweight?

I have asked this question on the Go mailing list and the members of the Go team (notably Dmitry Vyukov and Ian Lance Taylor) have been incredibly helpful.

The summary is:

  • stack that grows and shrinks on demand
  • smaller context, easier to switch
  • cooperative scheduling at known points is less work (can make assumptions about CPU state)
  • scheduling in user space

Here is the discussion: enjoy!

Note: There are other languages (such as Haskell and Erlang) that also have the concept of lightweight "processes" that allow for blocking calls without blocking OS threads. The concept is sometimes referred to as green threads.

Posted by Martin Konicek on 11:21 AM

23 comments:

Pedro Pedruzzi said...

Thanks for the nice post about go's novel approach to concurrency. It is awesome that go seem to have implemented this very efficiently and it is automatically used on the most frequent blocking situations: socket operations and locking (eg. using in-memory message queues)

It's worth mentioning that this concept is also known as fibers or lightweight threads. Some related links:
await implementation for Java:
https://github.com/electronicarts/ea-async
Fiber implementation for the JVM
http://docs.paralleluniverse.co/quasar/
Fiber implementation for Java
http://www.malhar.net/sriram/kilim/

Martin Konicek said...

Those are very good links, thank you Pedro! Ah yes, I remember hearing the name fibers too. I've been recently using the Hack language at fb which has the async / await feature similar to C#. My guess is the Hack runtime / libraries do something similar to Go under the hood, you can e.g. spawn a 1000 async operations and await all of them.

Blog FastCare said...

Nice i hope you will have many post like this
--------------
i'm providing thay man hinh Xiaomi service, special thay man hinh Xiaomi mi4. Contact with me

Aashi siva said...

A nice article here, i think that people who have grown up with the idea of using computers are showing more responsibility towards writing posts that are thoughtful, do not have grammar mistakes and pertinent to the post..
Salesforce Training in Chennai
PHP Training in Chennai
Informatica Training in Chennai

sai said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
python course in pune
python course in chennai
python course in Bangalore

john brito said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 

rpa training in chennai
rpa training in bangalore
rpa course in bangalore
best rpa training in bangalore
rpa online training

hansika hans said...

Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
Java training in Bangalore | Java training in Btm layout

Java training in Bangalore |Java training in Rajaji nagar

Java training in Bangalore | Java training in Kalyan nagar

Java training in Bangalore | Java training in Kalyan nagar

amala jst said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

kevin antony said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

saran latha said...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

sai said...

I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
python Online training in chennai
python Online training in bangalore
python interview question and answers

ragini ragini said...

Read all the information that i've given in above article. It'll give u the whole idea about it.
Online DevOps Certification Course - Gangboard
Best Devops Training institute in Chennai

High Technologies Solutions said...

Thanks for sharing this valuable information about Python with us, it is really helpful article!

cathryn leno said...

This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
angularjs Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

angularjs-Training in pune

jazz jerry said...

I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option? There are so many choices out there that I’m completely confused. Any suggestions? Thanks a lot.
AWS Training in Bangalore electronic city| AWS Training in Bangalore BTM Layout
AWS Training in Pune with placements | AWS Training in Pune kharadi
AWS Training in Chennai review |Best AWS Training in Chennai tnagar
Best AWS Amazon Web Services Training in Chennai | Best AWS Training centers in Chennai

sasitamil said...
This comment has been removed by the author.
saranyaregan said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
rpa training in bangalore
best rpa training in bangalore
rpa training in pune

jai said...

I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
Data Science Training in Chennai
Data Science training in kalyan nagar
Data science training in Bangalore
Data Science training in marathahalli
Data Science interview questions and answers
Data science training in jaya nagar
Data science training in bangalore

Gowtham said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

Post a Comment