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

70 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

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

Unknown 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

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

High Technologies Solutions said...

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

Unknown 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

Jagadeesh said...

Thanks for sharing this Valuable information. I like this blog.
Learn Digital Academy offers, Digital marketing training in Bangalore 
In-class training program, practically on Live Projects.

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Ajish said...

Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
Angular js Training in Electronic City

divi said...

thanks for your details.it's very pretty and very useful thanks for it web design company in velachery

datasciencecourse said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

business analytics course

data analytics courses in mumbai

data science interview questions

data science course in mumbai

imexpert said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
business analytics course

AI course in mumbai

imexpert said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

best digital marketing courses

j8899 said...



Excellent! I love to post a comment that "The content of your post is awesome" Great work!
business analytics courses

shanjames said...

Thank you for taking the time and sharing this information with us
Python Training In Hyderabad

nikhil reddy said...

Hi, Thanks for sharing wonderful blog post...

Data Science Training In Hyderabad

nikhil reddy said...

Hi, Thanks for sharing wonderful stuff...

AI Training In Hyderabad

devi said...

It is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog. Please, continue to give me such valuable posts.

Data Science Training In Chennai

Data Science Online Training In Chennai

Data Science Training In Bangalore

Data Science Training In Hyderabad

Data Science Training In Coimbatore

Data Science Training

Data Science Online Training

lionelmessi said...

I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best posting.

DevOps Training in Hyderabad

data science courses said...

Generally, this information or details are generated from the wide varieties of sources such as text files, financial logs, instruments and sensors and multimedia forms. Drawing meaningful and valuable insights from this information require advanced algorithms and tools.
data science courses in bangalore

Quickbooks Expert said...

Nice Blog !
users can easily maintain all the business accounting processes through a single software. To get support for payroll update, installation, and download error, call us at Quickbooks Customer Support Number 1-877-751-0742 and get instant technical solutions to curb QuickBooks problems.

Rachel Ross said...

Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks, visit at QuickBooks Error 1328 Our team at QuickBooks Error 1328 is always ready to help and support their clients.

Lauren Kathy said...

Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks Error, visit at QuickBooks Error 1328 Our team is always ready to help and support their clients.

Buy Instagram pva said...

There are many important things in life to consider. Some of the most important things in life to consider are love, family, health, and survival. All of these things can be easily taken for granted, but they will be more appreciated if they are not taken for granted. There are also many important thingsBuy Bulk Twitter accounts in life that we sometimes take for granted but never get around to doing. Some examples of these would be: having a job, getting out of bed in the morning, getting groceries, and taking care of your children.Buy snapchat account

AnnaSereno said...


Hey! Lovely blog. Your blog contains all the details and information related to the topic. In case you are a QuickBooks user, here is good news for you. You may encounter any error like QuickBooks Error, visit at QuickBooks Support Phone Number for quick help.

Phoebe Geller said...

Hey! Mind-blowing blog. Keep writing such beautiful blogs. In case you are struggling with issues on QuickBooks software, dial QuickBooks Customer Service Phone Number . The team, on the other end, will assist you with the best technical services.

AnnaSereno said...

Hey! Well-written blog. It is the best thing that I have read on the internet today. Moreover, if you are looking for the solution of QuickBooks Software, visit at QuickBooks Customer Service (866)669-5068 to get your issues resolved quickly.

Robin said...

This is an informative blog. If you are looking for software that will improve your workplace. Feel free to visit Software to know more about our services.

Maneesha said...

I really enjoyed this blog. It's an informative topic. It helps me very much to solve some problems. Its opportunities are so fantastic and the working style so speedy.
data scientist training and placement in hyderabad

Harry Jackson said...

Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks Error, visit at QuickBooks Customer Service (602)325-1557. Our team is always ready to help and support their clients.

MindQ said...

Thanks for sharing with us
Online Training | Classroom | Virtual Classes
SDET Training with 100% placement assistance
1860 testers placed in 600 companies in last 8 years
SDET Training from Real-time expert trainers
Industry oriented training with corporate case studies
Free Aptitude classes & Mock interviews

Kristen Bell said...

Great work! It is the best thing that I have read on the internet today. Moreover, If you encounter any error while working on QuickBooks for MAC Support , do contact this QuickBooks Support Phone Number (855)552-2543 number for quick assistance.

Unknown said...

Hi buddies, it is great written piece entirely defined, continue the good work constantly. data science course in mysore

Unknown said...

Admiring the time and effort you put into your blog and detailed information you offer!.. business analytics course in surat

Unknown said...

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. business analytics course in surat

Unknown said...

It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it! data scientist course in kanpur

Quickbooks Support Service Phone Number.+1 877-603-0806,Illniois said...

Nice blog!!
if you are looking for Quickbooks customer serviceyou can reach us at.+1 877-603-0806,Illniois

Quickbooks Support Phone Number. +18884712380,VA. said...

Just dial +1 888-471-2380,VA. from wherever you are across the country to contact our reliable and trusted Quickbooks support center. You will get authentic solutions for your Quickbooks related issues to fulfill your bookkeeping requirements. Quickbooks Customer Service

PMP Training in Malaysia said...

360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

vcube said...

This information has been shared  is very helpful and instructive.
Learn Testing Tools Course In Hyderabad

Gayatri said...

This insightful write-up delves into the core differences between Go's goroutines and traditional OS threads, shedding light on their lightweight nature. It highlights key factors like stack management, cooperative scheduling, and user-space scheduling, providing a clear understanding of Go's concurrency model. Thank you for sharing this article.
Data Analytics Courses in Nashik

Pratyaksha said...

Such a helpful blog! Your post simplifies this complex topic and empowers developers to leverage the full potential of goroutines.

Data Analytics Courses In Chennai

Data Analytics Courses in Agra said...

What a great blog! Your article demystifies this complicated subject. Thanks for this wonderful post.
Data Analytics Courses in Agra

Digital marketing courses in illinois said...

I'm looking forward to exploring more about how to create efficient and concurrent applications in Go using goroutines. Keep up the excellent work in sharing your knowledge and insights.
Digital marketing courses in illinois

Gogou Misao said...

That was a great little solution. Thanks for posting.

Investment banking analyst jobs

Kenny Sen said...

When dealing with computationally intensive tasks, such as data analysis or mathematical computations, goroutines can be employed to perform parallel processing. This can lead to significant performance improvements by utilizing multiple CPU cores. Thank you for an amazing knowledge. Keep sharing.
Investment banking courses in the world

vv software said...

Nice blog... Good explanation please keep posting
Java training in JNTU

DigiperformSeo said...

Your post is very great. I read this post this is very helpful. I will definitely go ahead and take advantage of this. Want to know about Best Data Science Course In Chandigarh By Digiperform

Post a Comment