We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
0 parents commit cf99b54Copy full SHA for cf99b54
parallel.go
@@ -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