Skip to content

Instantly share code, notes, and snippets.

@hayajo
Last active August 29, 2015 13:56
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 hayajo/9248417 to your computer and use it in GitHub Desktop.
Save hayajo/9248417 to your computer and use it in GitHub Desktop.
ゴルーチンをプールするパターン
package main
import (
"fmt"
"sync"
"time"
)
func main() {
pool := make(chan bool, 3)
job := make(chan string)
go func() {
for {
fmt.Println(<-job)
}
}()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
pool <- true
wg.Add(1)
go func(i int) {
defer func() { <-pool }()
job <- fmt.Sprintf("start job #%d", i)
time.Sleep(time.Second)
job <- fmt.Sprintf("end job #%d", i)
wg.Done()
}(i)
}
wg.Wait()
}
@hayajo
Copy link
Author

hayajo commented Feb 27, 2014

こんなかんじ

@hayajo
Copy link
Author

hayajo commented Mar 9, 2014

まちがってた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment