mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
update book code
This commit is contained in:
31
eBook/examples/chapter_14/max_tasks.go
Normal file
31
eBook/examples/chapter_14/max_tasks.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
const MAXREQS = 50
|
||||
var sem = make(chan int, MAXREQS)
|
||||
|
||||
type Request struct {
|
||||
a, b int
|
||||
replyc chan int
|
||||
}
|
||||
|
||||
func process(r *Request) {
|
||||
// do something
|
||||
}
|
||||
|
||||
func handle(r *Request) {
|
||||
sem <- 1 // doesn't matter what we put in it
|
||||
process(r)
|
||||
<-sem // one empty place in the buffer: the next request can start
|
||||
}
|
||||
|
||||
func server(service chan *Request) {
|
||||
for {
|
||||
request := <-service
|
||||
go handle(request)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
service := make(chan *Request)
|
||||
go server(service)
|
||||
}
|
Reference in New Issue
Block a user