-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.go
58 lines (48 loc) · 1.24 KB
/
thread.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package audioc
import (
"github.com/jamlib/audioc/metadata"
)
// process each audio file within bundle in separate cpu thread
func (a *audioc) processThreaded(indexes []int) ([]*metadata.Metadata, error) {
var err error
jobs := make(chan int)
data := make(chan []metadata.Metadata, a.Workers)
// iterate through files sending them to worker processes
go func() {
for x := range indexes {
if err != nil {
break
}
jobs <- indexes[x]
}
// close jobs channel once out of indexes
close(jobs)
}()
// start worker processes
for i := 0; i < a.Workers; i++ {
go func() {
// build metadata slice
md := make([]metadata.Metadata, 0, len(indexes))
for job := range jobs {
m, e := a.processFile(job)
md = append(md, *m)
// if single job errors, break & set shared err
if e != nil {
err = e
break
}
}
data <- md
}()
}
// wait for all workers to finish
results := make([]*metadata.Metadata, 0, len(indexes))
for i := 0; i < a.Workers; i++ {
workerMetadata := <-data
for x := range workerMetadata {
results = append(results, &workerMetadata[x])
}
}
close(data)
return results, err
}