Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 675
Frank Avatar asked Nov 16 '09 01:11

Frank


People also ask

Does go use pthreads?

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.

Is Golang multithreaded?

With Go, it's possible to do multi-threaded concurrency and parallelization with goroutines and goroutines work in an asynchronous way hence making use of both multi-threading and asynchronous programming efficiently.

What is pthread in Java?

POSIX Threads are commonly known as PThreads. It is an execution model that exists independently from a language and a parallel execution model. It allows a program to control multiple different workflows that overlap in time.


2 Answers

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.

like image 133
Randy Sugianto 'Yuku' Avatar answered Sep 17 '22 20:09

Randy Sugianto 'Yuku'


IMO, what makes the multi-threading in Go appealing is the communication facilities: unlike pthread where one must build the communications infrastructure (mutex, queues etc.), in Go it is available by default in a convenient form.

In short, there is "low-friction" to using threads because of the good communication facilities (akin to Erlang if I can say so).

like image 21
jldupont Avatar answered Sep 17 '22 20:09

jldupont