sync

package
v1.0.32 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrSizeLessZero = errors.New("size less than 0")

Functions

This section is empty.

Types

type RoutineGroup

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

RoutineGroup 协程组,是sync.WaitGroup的增强版本.

func NewRoutineGroup

func NewRoutineGroup(size uint) *RoutineGroup

NewRoutineGroup 协程组,控制协程总数量.

Example
package main

import (
	"fmt"

	"github.com/xuender/kit/sync"
)

func main() {
	group := sync.NewRoutineGroup(3)
	for i := 0; i < 5; i++ {
		group.Add(1)

		go func() {
			defer group.Done()

			fmt.Println("x")
		}()
	}

	group.Wait()

}
Output:
x
x
x
x
x

func (*RoutineGroup) Add

func (p *RoutineGroup) Add(delta uint)

Add 增加协程.

func (*RoutineGroup) Done

func (p *RoutineGroup) Done()

Done 协程完成.

func (*RoutineGroup) Inc

func (p *RoutineGroup) Inc()

Inc 加1.

Example
package main

import (
	"fmt"

	"github.com/xuender/kit/sync"
)

func main() {
	group := sync.NewRoutineGroup(3)
	for i := 0; i < 5; i++ {
		group.Inc()

		go func() {
			defer group.Done()

			fmt.Println("x")
		}()
	}

	group.Wait()

}
Output:
x
x
x
x
x

func (*RoutineGroup) Wait

func (p *RoutineGroup) Wait()

Wait 等待全部完成.

Jump to

Keyboard shortcuts

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