C++11 threads tutorial

  • Is C++ usage on the decline? I've used a lot of different languages, but I've never had the occasion to learn C++. I work in web search and we tend to use interpreted languages. The argument is that Java or C# is much easier to write/maintain and that we can make up for performance shortcomings by scaling out.

  • 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