Difference Between Goroutines and Threads

In Golang Concurrency Part I, I said Goroutines are not threads. In this post I will explain the same.

Size

OS threads are basically fixed size stack, preferably 2 MB. It’s a huge space  e.g. invoking 1024 threads will result in 2 GB space occupancy.

A Goroutine starts life with a small stack, typically 2 KB. Because some go routines are too small for a 2 MB stack and stack of Goroutine is growable.

The size limit for a Goroutine stack may be as much as 1GB, so thread is limited for recursive operation because recursive stack greater then 2 MB will cause, stack overflow. Go o the other hand can grow it’s stack to 1 GB, WOW, now huge recursion is possible in Go.

Scheduling

OS threads are scheduled by kernel. Hardware timer dependency.

Go run-time has its own scheduler. No hardware timer dependency. It’s a m:n scheduler. Means, m (Goroutines) => n ( Threads).

 

Identity

Goroutines has no identity, for some it must be an issue, but I can easily live with that. Go has this implemented because Goroutines has  no TLS  (Thread local storage).

Happy coding.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s