Skip to content

Commit cf99b54

Browse files
committed
Init
0 parents  commit cf99b54

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

parallel.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package parallel
2+
3+
type Input []interface{}
4+
type Output []interface{}
5+
type Callback func(interface{}) interface{}
6+
type Processor struct {
7+
Threads int
8+
}
9+
10+
func (ap *Processor) Process(input Input, callback Callback) Output {
11+
var out Output
12+
13+
jobs := make(chan interface{}, len(input))
14+
results := make(chan interface{}, len(input))
15+
16+
for t := 1; t <= ap.Threads; t++ {
17+
go func(id int, jobs chan interface{}, out interface{}) {
18+
for job := range jobs {
19+
results <- callback(job)
20+
}
21+
}(t, jobs, results)
22+
}
23+
24+
for _, i := range input {
25+
jobs <- i
26+
}
27+
close(jobs)
28+
29+
for a := 1; a <= len(input); a++ {
30+
out = append(out, <-results)
31+
}
32+
33+
return out
34+
}

0 commit comments

Comments
 (0)