sync

package
v1.0.47 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrGroupLessZero = errors.New("group less than 0")

Functions

This section is empty.

Types

type Map added in v1.0.40

type Map[K comparable, V any] struct{ sync.Map }

func NewMap added in v1.0.40

func NewMap[K comparable, V any](items ...lo.Tuple2[K, V]) *Map[K, V]
Example
package main

import (
	"fmt"

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

func main() {
	smap := sync.NewMap[int, int]()

	smap.Store(1, 1)
	fmt.Println(smap.Load(1))
	fmt.Println(smap.LoadOrCreate(2, func() int { return 2 }))

}
Output:

1 true
2 false

func (*Map[K, V]) Delete added in v1.0.40

func (p *Map[K, V]) Delete(key K)

func (*Map[K, V]) Load added in v1.0.40

func (p *Map[K, V]) Load(key K) (V, bool)

func (*Map[K, V]) LoadAndDelete added in v1.0.40

func (p *Map[K, V]) LoadAndDelete(key K) (V, bool)

func (*Map[K, V]) LoadOrCreate added in v1.0.40

func (p *Map[K, V]) LoadOrCreate(key K, create func() V) (V, bool)

func (*Map[K, V]) LoadOrStore added in v1.0.40

func (p *Map[K, V]) LoadOrStore(key K, value V) (V, bool)

func (*Map[K, V]) Range added in v1.0.40

func (p *Map[K, V]) Range(call func(key K, value V) bool)

func (*Map[K, V]) Store added in v1.0.40

func (p *Map[K, V]) Store(key K, value V)

type RoutineGroup

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

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

func NewRoutineGroup

func NewRoutineGroup(group int32) *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.Incr()

		go func() {
			defer group.Done()

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

	group.Wait()

}
Output:

x
x
x
x
x

func (*RoutineGroup) Done

func (p *RoutineGroup) Done()

Done 协程完成.

func (*RoutineGroup) Incr added in v1.0.46

func (p *RoutineGroup) Incr()

Incr 加1.

Example
package main

import (
	"fmt"

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

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

		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