SoFunction
Updated on 2025-03-05

Write a concurrent package using Go language

Write in front

This is an extremely simple (simple principle, simple usage, and simple function) go package that only needs to be implemented with 50 lines of code (the core code is only 15 lines) and can be implemented with simple principles (simple principles, simple usage, and simple functions)mini_parallel_job, suitable for most concurrent tasks, out of the box.

Code

package mini_parallel_job

import (
    "fmt"
    "sync"
)

type JobType func()

type JobPool interface {
    AddJob(jobType JobType)
    Wait()
}

type jobPool struct {
    jobs []JobType
}

// Add a taskfunc (j *jobPool) AddJob(job JobType) {
     = append(, job)
}

// Start and wait for the taskfunc (j *jobPool) Wait() {
    var wg 
    (len())
    for i := range  {
       jJob := [i]
       go func() {
          defer func() {
             ()
             if err := recover(); err != nil {
                ("err:%+v", err)
             }
          }()

          jJob()
       }()
    }
    ()
}

func NewJobPool() JobPool {
    return &jobPool{
       jobs: make([]JobType, 0),
    }
}

Pressure test

package mini_parallel_job

import (
    "testing"
)

const (
    Count = 10
)

// Parallel tasksfunc parallelJob() {
    jobPool := NewJobPool()
    for i := 0; i < Count; i++ {
       (func() {
          _ = fib(10)
       })
    }
    ()
}

// Serial Taskfunc serialJob() {
    for i := 0; i < Count; i++ {
       _ = fib(10)
    }
}

// Taskfunc fib(n int) int {
    if n == 0 || n == 1 {
       return n
    }
    return fib(n-2) + fib(n-1)
}

// Performance testfunc BenchmarkSerialJob(b *) {
    for i := 0; i < ; i++ {
       serialJob()
    }
}

func BenchmarkParallelJob(b *) {
    for i := 0; i < ; i++ {
       parallelJob()
    }
}

/*
BenchmarkSerialJob-12             298855              3756 ns/op
BenchmarkParallelJob-12           117189              8710 ns/op
*/

example

package main

import (
    "fmt"
    mini_parallel_job "mini-parallel-job"
    "time"
)

const (
    JobCount = 10
)

func main() {
    // Serial execution    begin1 := ()
    for i := 0; i < JobCount; i++ {
       fib(40)
    }
    ((begin1))

    // Parallel execution    begin2 := ()
    parallelJob := mini_parallel_job.NewJobPool()
    for i := 0; i < JobCount; i++ {
       (func() {
          fib(40)
       })
    }
    ()
    ((begin2))

    /*
        result:
        7.335989407s
        1.112108503s
     */
}

// Taskfunc fib(n int) int {
    if n == 0 || n == 1 {
       return n
    }
    return fib(n-2) + fib(n-1)
}

Summarize

This code is only usedgo rountineImplement concurrency,Implement waiting.

In most scenarios, only concurrency is needed and not care about the amount of concurrency. Most programmers also use it.WaitThe function code is implemented (at least the author saw this in the project, and there are many identical codes, which were encapsulated based on this scenario).

If you want to implement more complex scenarios, such as controlling the maximum concurrency, you can make some modifications to the above code.WaitAdd a specified size to the functionchanTo control it. Or refer to the author's other concurrency package for gogopool, concurrent control implemented using master-worker mode.

This is the end of this article about writing a concurrent package using the Go language. For more related content on the go concurrent package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!