-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreads.go
More file actions
44 lines (34 loc) · 1021 Bytes
/
threads.go
File metadata and controls
44 lines (34 loc) · 1021 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
42
43
44
package threads
import (
"context"
"sync"
)
type Thread interface {
// SetWait sets a wait group to use when executing the thread's function. It will add to it
// before starting the function and call done when the function completes.
SetWait(*sync.WaitGroup)
// GetCompleteChannel returns a channel that will have the function's result written to it when
// the thread's function completes.
GetCompleteChannel() <-chan error
// Start starts execution of the thread's function.
Start(context.Context)
// IsComplete returns true if the thread has completed executing.
IsComplete() bool
// Error returns any error returned from execution. Only call after a thread is complete.
Error() error
}
type Threads []Thread
func (ts Threads) Start(ctx context.Context) {
for _, thread := range ts {
thread.Start(ctx)
}
}
func (ts Threads) Errors() []error {
var result []error
for _, thread := range ts {
if err := thread.Error(); err != nil {
result = append(result, err)
}
}
return result
}