Created
February 5, 2015 04:18
-
-
Save suzuken/363c260de038c06e0848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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