Last active
July 5, 2016 07:57
-
-
Save catatsuy/4b4ef8f0fe7b547ac73a25cfeba21d05 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
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