Skip to content

Commit 6307d62

Browse files
committed
Merge branch 'rc' of github.com:duke-git/lancet into rc
2 parents 2f9f8b3 + db5d940 commit 6307d62

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

slice/slice_concurrent.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func ReduceConcurrent[T any](slice []T, initial T, reducer func(index int, item
125125
func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
126126
result := make([]T, 0)
127127
var wg sync.WaitGroup
128+
var mu sync.Mutex
129+
128130

129131
workerChan := make(chan struct{}, numThreads)
130132

@@ -137,7 +139,9 @@ func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool,
137139
defer wg.Done()
138140

139141
if predicate(i, v) {
142+
mu.Lock()
140143
result = append(result, v)
144+
mu.Unlock()
141145
}
142146

143147
<-workerChan

0 commit comments

Comments
 (0)