views:

355

answers:

2

We've been talking about threads in my operating system class a lot lately and one question has come to my mind.

Since Go, (and Java) uses User-space thread instead of kernel threads, doesn't that mean that you can't effectively take advantages of multiple cores since the OS only allocates CPU time to the process and not the threads themselves?

This seems to confirm the fact that you can't

Wikipedia also seems to think so

+6  A: 

Most recent versions of Java to use OS threads, although there is not necessarily a one-to-one mapping with Java threads. Java clearly does work quite nicely across many hardware threads.

Tom Hawtin - tackline
So basically, the JVM creates kernel threads with what would be supposed to be user threads?
Gab Royer
Basically, yes.
Stephen C
+13  A: 

What makes you think Go uses User-space threads?

It doesn't. It uses OS-threads and can take advantage of multiple cores.

You might be puzzled by the fact that by default Go only uses 1 thread to run your program. If you start two goroutines they run in one thread. But if one goroutine blocks for I/O Go creates a second thread and continues to run the other goroutine on the new thread.

If you really want to unlock the full multi-core power just use the GOMAXPROCS() function.

runtime.GOMAXPROCS(4); //somewhere in main

Now your program would use 4 OS-threads (instead of 1) and would be able to fully use a e.g. 4 core system.

jitter
For some reason, I though I read it somewhere...
Gab Royer
And now can't seem to find where, oh well, guess you're right then!
Gab Royer
But otherwise you were right. If something really relies on User-space threads it won't be able to use multi-core/multi-cpu systems fully
jitter