4 ms·
For C++, that's actually not too bad. Now try creating 100,000 threads with synchronized communication. In Go it's trivial: $ cat goroutines.go package
by jbarham 15y ago
For C++, that's actually not too bad. Now try creating 100,000 threads with synchronized communication. In Go it's trivial:
$ cat goroutines.go
package main
import (
"flag"
"fmt"
)
var n *int = flag.Int("n", 100000, "thread count")
func thread(x int, result chan int) {
result <- x
}
func main() {
flag.Parse()
results := make(chan int)
for i := 0; i < *n; i++ {
go thread(i, results)
}
for i := 0; i < *n; i++ {
fmt.Println(<-results)
}
}
$ time ./goroutines | tail
99990
99991
99992
99993
99994
99995
99996
99997
99998
99999
real 0m0.723s
user 0m0.396s
sys 0m0.840s
http://weekly.golang.org/doc/effective_go.html#concurrency http://weekly.golang.org/doc/effective_go.html#concurrency
- ramy_d 15y agoI feel like your comment ignores what languages are for and what people looking for a c++11 tutorial on threading already know. You might think an equally trivial example with erlang is in order, but it totally marginalizes why anyone chooses to write with any given language in the first place.