1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

Update slice_concurrent.go

This commit is contained in:
jake
2025-06-24 19:14:37 +08:00
committed by GitHub
parent a3a24fc381
commit 83c069e234

View File

@@ -125,6 +125,8 @@ func ReduceConcurrent[T any](slice []T, initial T, reducer func(index int, item
func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
result := make([]T, 0)
var wg sync.WaitGroup
var mu sync.Mutex
workerChan := make(chan struct{}, numThreads)
@@ -137,7 +139,9 @@ func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool,
defer wg.Done()
if predicate(i, v) {
mu.Lock()
result = append(result, v)
mu.Unlock()
}
<-workerChan