-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
41 lines (34 loc) · 962 Bytes
/
pool.go
File metadata and controls
41 lines (34 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// A simple goroutine pool for Golang
package goroutinepool
// Command: Implement the Command interface by providing an Exec() function in your structs.
// This Exec() function encapsulates the logic that you want to execute in goroutines.
type Command interface {
Exec()
}
// A helper struct to manage the pool
type Pool struct{}
// The Run() function takes
// numOfIterations: number of times this command should execute
// concurrency: The pool size or the maximum number of goroutines that should execute at any given time
// cmd: The implementation of the goroutinepool.Command interface
func (pool *Pool) Run(numOfIterations int, concurrency int, cmd Command) {
c := make(chan int, concurrency)
cnt := make(chan bool)
counter := 0
for i := 0; i < numOfIterations; i++ {
c <- 1
go func(cmd Command) {
cmd.Exec()
<-c
cnt <- true
}(cmd)
}
for {
if <-cnt {
counter++
if counter == numOfIterations {
break
}
}
}
}