Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created February 5, 2015 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuken/363c260de038c06e0848 to your computer and use it in GitHub Desktop.
Save suzuken/363c260de038c06e0848 to your computer and use it in GitHub Desktop.
// original: http://mattn.kaoriya.net/software/lang/go/20140625223125.htm
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
l := new(sync.Mutex)
c := sync.NewCond(l)
// at first, creating 10 goroutine.
for i:= 0; i < 10; i++ {
go func(i int) {
fmt.Printf("waiting %d\n", i)
l.Lock()
defer l.Unlock()
c.Wait()
fmt.Printf("go %d\n", i)
}(i)
}
// after, process one by one
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
// Signal dispatch each routines.
c.Signal()
}
time.Sleep(1 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment