5 examples of 'golang parallel' in Go

Every line of 'golang parallel' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
45func (m *TaskRunners) Parallel(name string) plan.Task {
46 return exec.NewTaskParallel(m.ctx, name)
47}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

53func Parallel(xs []uint64, k uint64) int16 {
54 cpus := runtime.NumCPU()
55 if cpus%2 != 0 {
56 panic(fmt.Sprintf("odd number of CPUs %v", cpus))
57 }
58 sz := len(xs)/cpus + 1
59 var wg sync.WaitGroup
60 retChan := make(chan int16, cpus)
61 for i := 0; i < len(xs); i += sz {
62 end := i + sz
63 if end >= len(xs) {
64 end = len(xs)
65 }
66 chunk := xs[i:end]
67 wg.Add(1)
68 go func(hd int16, xs []uint64, k uint64, wg *sync.WaitGroup, ch chan int16) {
69 for i := 0; i < len(xs); i += 2 {
70 if xs[i] >= k {
71 ch <- (int16(i) + hd) / 2
72 break
73 }
74 }
75 wg.Done()
76 }(int16(i), chunk, k, &wg, retChan)
77 }
78 wg.Wait()
79 close(retChan)
80 var min int16 = (1 << 15) - 1
81 for i := range retChan {
82 if i < min {
83 min = i
84 }
85 }
86 if min == (1<<15)-1 {
87 return int16(len(xs) / 2)
88 }
89 return min
90}
52func processParallel(ctx context.Context, requests []Request) error {
53 g, ctx := errgroup.WithContext(ctx)
54 for _, req := range requests {
55 // Need to use a separate func since g.Go doesn't take input
56 req := req
57 func() {
58 g.Go(func() error {
59 return req.Process(ctx)
60 })
61 }()
62 }
63
64 return g.Wait()
65}
127func (w *worker) Parallel(fn func()) {
128 w.wg.Add(1)
129 w.ch <- workUnit{wg: w.wg, fn: fn}
130}
115func parallelComponent() *charts.Parallel {
116 parallel := charts.NewParallel()
117 parallel.SetGlobalOptions(
118 charts.TitleOpts{Title: "Parallel-ParallelComponent"},
119 charts.ParallelComponentOpts{
120 Left: "15%",
121 Right: "13%",
122 Bottom: "10%",
123 Top: "20%",
124 },
125 )
126 parallel.ParallelAxisOpts = parallelAxis
127 parallel.Add("北京", parallelDataBJ)
128 return parallel
129}

Related snippets