M
M
Michael2016-03-01 06:52:47
go
Michael, 2016-03-01 06:52:47

How to check spawned goroutines?

Hello, here is a test fiddle

package main

func gorutine(){
  for{
    time.Sleep(10000*time.Second)
  }
}

func main() {
  sum := 1
  for sum < 1000 {
    sum += sum
    go gorutine()
  }
    //ждем пока они выспятся все
}

it's simple, we create 1000 goroutines, and they all sleep, so WHERE ARE THEY ALL ?? well, that is htop they are not visible, ps-ejH too, how to see where all these 1000 lazy people are?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fastpars, 2016-03-01
@nextel

Hello.
1. Goruntine != thread/process. Go decides whether it needs a thread/process.
2. Your example is executed instantly. goroutines do not block the execution flow of the main program.
To "wait for all gorutines to execute", you can use channels or a WaitGroup from the sync package.
3. Your loop will only run 10 times. "sum += sum" and not "sum++" and not "sum = sum + 1"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question