goroutinepool

package module
v0.0.0-...-b7879b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2022 License: MIT Imports: 1 Imported by: 0

README

goroutinepool

Simple Goroutine pool
一个简单的go golang 协程池
使用原生的channel和waitgroup实现,简单实用

How to use

go get gitee.com/JxGolibs/goroutinepool

Simple example


package main

import (
	"gitee.com/JxGolibs/goroutinepool"
	"time"
	"fmt"
)

func main() {

	p := goroutinepool.NewRoutinePool(10)
	for i := 0; i < 100; i++ {
		count := i
		p.AddJob(func() {
			time.Sleep(1000 * time.Millisecond)
			fmt.Printf("%d\n", count)
		})

	}
	p.Wait()
}

Test Parallel Sum


package main

import (
	"github.com/pkufergus/goroutinepool"
	"time"
	"fmt"
	"sync"
)

func main() {

	p := goroutinepool.NewRoutinePool(10)
	g_index := 0
	var lock *sync.Mutex
	lock = new(sync.Mutex)
	for i := 0; i < 100; i++ {
		count := i
		p.AddJob(func() {
			time.Sleep(1000 * time.Millisecond)
			fmt.Printf("%d\n", count)
			lock.Lock()
			defer lock.Unlock()
			g_index++
		})

	}
	p.WaitAll()
	fmt.Printf("sum=%d\n", g_index)
}

Doc

详细的文档 https://blog.csdn.net/pkufergus/article/details/86495914

后续

1、增加参数传递
2、增加返回参数传递
3、增加超时控制

问题相关

有任何问题欢迎提Issue,或者将问题描述发送至我邮箱 caosong1319@gmail.com.我会尽快解答.推荐提交Issue方式.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func()

type RoutinePool

type RoutinePool struct {
	// contains filtered or unexported fields
}

func NewRoutinePool

func NewRoutinePool(max int) *RoutinePool

func (*RoutinePool) AddJob

func (rp *RoutinePool) AddJob(job Job)

func (*RoutinePool) Wait

func (rp *RoutinePool) Wait()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL