What is the difference between Go's multithreading and pthread or Java Threads?

Frank picture Frank · Nov 16, 2009 · Viewed 16k times · Source

What is the difference between Go's multithreading approach and other approaches, such as pthread, boost::thread or Java Threads?

Answer

Randy Sugianto 'Yuku' picture Randy Sugianto 'Yuku' · Nov 16, 2009

Quoted from Day 3 Tutorial <- read this for more information.

Goroutines are multiplexed as needed onto system threads. When a goroutine executes a blocking system call, no other goroutine is blocked.

We will do the same for CPU-bound goroutines at some point, but for now, if you want user-level parallelism you must set $GOMAXPROCS. or call runtime.GOMAXPROCS(n).

A goroutine does not necessarily correspond to an OS thread. It can have smaller initial stack size and the stack will grow as needed.

Multiple gorouitines may be multiplexed into a single thread when needed.

More importantly, the concept is as outlined above, that a goroutine is a sequential program that may block itself but does not block other goroutines.

Goroutines is implemented as pthreads in gccgo, so it can be identical to OS thread, too. It's separating the concept of OS thread and our thinking of multithreading when programming.