Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you detect how many threads a given number of goroutines will create?

Tags:

go

goroutine

I understand that goroutines are multiplexed onto multiple OS threads, so if one should block, such as while waiting for I/O, others continue to run. But is there any way to know ahead of time how many threads I would spawn if I was to create n goroutines?

For example, if we call the function below would we know how many (or the maximum number of) system threads would be created for n goroutines:

type Vector []float64

// Apply the operation to n elements of v starting at i.
func (v Vector) DoSome(i, n int, u Vector, c chan int) {
    for ; i < n; i++ {
        v[i] += u.Op(v[i])
    }
    c <- 1;    // signal that this piece is done
}
like image 503
Brad The App Guy Avatar asked Nov 11 '09 09:11

Brad The App Guy


People also ask

How many Goroutines can be created?

In the case of goroutines, since stack size can grow dynamically, you can spawn 1000 goroutines without a problem. As a goroutine starts with 8KB (2KB since Go 1.4) of stack space, most of them generally don't grow bigger than that.

Are Goroutines user level threads?

goroutines are what we are most familiar with in Go, and could be considered user threads. A more technical name for those is Green Threads. P is used to perform the mapping from many goroutines to many OS threads.

How many threads does Golang create?

For one thing, each thread takes up a certain amount of memory resources, and a large number of threads can lead to memory exhaustion, and the Go runtime actually has a display for the number of threads created at runtime, which is 10000 by default.

Are Goroutines processes or threads?

A goroutine is a lightweight thread managed by the Go runtime.


2 Answers

Each goroutine can use a maximum of one thread at a time. Whether it uses a thread or not depends on what it's doing. The value of GOMAXPROCS determines the number of threads that can be used by freely running Go code - in other words, the maximum level of parallelism.

However more threads can be used, even with GOMAXPROCS=1, when goroutines block directly on system calls or calls into C.

The following operations do not cause the goroutine to use a thread when they block:

  • channel operations
  • network operations
  • sleeping
  • all primitives in the sync package

This means, for example, that if you have many goroutines that open /dev/ttyxx and block on read, you'll be using a thread for each one. Same goes if you're execing a load of processes and waiting for them to exit.

like image 115
rog Avatar answered Sep 19 '22 19:09

rog


According to Pike's Go Course PDF slides (Day 3):

...if you want user-level parallelism you must set $GOMAXPROCS or call runtime.GOMAXPROCS(n). GOMAXPROCS tells the runtime scheduler how many non-syscall-blocked goroutines to run at once.

Based on this blog post, too, it would seem setting the environment variable GOMAXPROCS lets you fix the number of threads. I'm not sure how to get the default number of threads the runtime will manage if you do not specify this value, however.

This blog post seems to imply that if you do not set the environment variable the runtime will only utilize one core (presumably because it is only using one process.)

like image 26
fbrereto Avatar answered Sep 21 '22 19:09

fbrereto