Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Last active July 5, 2016 07:57
Show Gist options
  • Save catatsuy/4b4ef8f0fe7b547ac73a25cfeba21d05 to your computer and use it in GitHub Desktop.
Save catatsuy/4b4ef8f0fe7b547ac73a25cfeba21d05 to your computer and use it in GitHub Desktop.
package main
import (
"sync"
"time"
)
type clothes struct {
Price int
Folded bool
}
type commer struct {
sync.Mutex
Goods []*clothes
}
func (c *clothes) Fold() {
c.Folded = true
}
func main() {
// とりあえず100人くらい並べる
tasks := make(chan commer, 100)
go func() {
for {
tasks <- commer{Goods: []*clothes{&clothes{Price: 1000, Folded: false}, &clothes{Price: 2000, Folded: false}, &clothes{Price: 1500, Folded: false}}}
}
}()
// レジには5人
semaphore := make(chan struct{}, 5)
for i := 0; i < 200; i++ {
go func() {
semaphore <- struct{}{}
task := <-tasks
sum := 0
for _, c := range task.Goods {
sum += c.Price
}
folded := make(chan struct{})
go func() {
task.Lock()
for _, cl := range task.Goods {
cl.Fold()
}
task.Unlock()
folded <- struct{}{}
}()
// お金を払う
<-folded
<-semaphore
}()
}
// goroutineの終了は待たない
time.Sleep(2 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment