Skip to content

Instantly share code, notes, and snippets.

@Geal
Created July 12, 2014 14:55
Show Gist options
  • Save Geal/f9279854b64c2bc8f002 to your computer and use it in GitHub Desktop.
Save Geal/f9279854b64c2bc8f002 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"time"
"sync"
)
func main() {
fmt.Println("ready? start!")
runtime.GOMAXPROCS(4)
var state = 0
var send = make(chan int)
var receive = make(chan int)
var racers = 10
var laps = 10
var wg sync.WaitGroup
fmt.Println("lauching race")
//manager
go func() {
send <- state
for {
select {
case current := <-receive:
state = current
case <-time.After(5 * time.Millisecond):
send <- state
}
}
}()
wg.Add(racers)
for w := 0; w < racers; w++ {
go func() {
defer wg.Done()
for x := 0; x < laps; x++ {
current := <-send
current = current + 1
fmt.Println(current)
time.Sleep(10 * time.Millisecond)
receive <- current
time.Sleep(4 * time.Millisecond)
}
}()
fmt.Println("launched ", w)
}
wg.Wait()
fmt.Println("state:", state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment