Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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