On 2012/7/24 <
xiez...@gmail.com> wrote:
> run package main
>
> import (
> "fmt";
> "runtime";
> "strconv"
> )
>
> func main() {
> runtime.GOMAXPROCS(2)
> ch := make(chan int)
> n := 1000
> for i := 0; i < n; i++ {
> task(strconv.Itoa(i), ch, 100)
> }
> fmt.Printf("begin\n")
> for i := 0; i < n; i++ {
> <-ch
> }
> }
>
> func task(name string, ch chan int, max int) {
> go func() {
> i:= 1
> for i <= max {
> fmt.Printf("%s %d\n", name, i)
> //print(name + " " + strconv.Itoa(i) + "\n")
> i++
> }
> ch <- 1
> }();
> }
>
>
>
> $ ./par | less
>
> then ls /proc/<pid>/tasks | wc -l ,will output 1002
>
> strace -f ./par | less will also see so many clone sys call.
This is because your goroutines are concurrently calling Printf, which
is a blocking syscall. These are managed in different threads and if
you have 1000 of them outstanding at the same time, you get 1000
threads.
Rémy.