golock

package
v1.2.49 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

Go-LOCK

  • 用法
package main

import (
	"fmt"
	golock "github.com/gif-gif/go.io/go-lock"
	gopool "github.com/gif-gif/go.io/go-pool"
	"github.com/gif-gif/go.io/goio"
)

func main() {
	goio.Init(goio.DEVELOPMENT)
	testGoPool()
	//testSyncLock()
}

func testGoPool() {
	var (
		count int
	)
	lock := golock.New()
	//并发池
	gp := gopool.NewFixedSizePool(10, 10)
	defer gp.StopAndWait()
	group := gp.NewTaskGroup()
	for i := 0; i < 10; i++ {
		group.Submit(func() {
			for i := 1000; i > 0; i-- {
				lock.WLockFunc(func(parameters ...any) {
					count++
				})
			}
			fmt.Println(count)
		})
	}
	group.Wait()
}

func testSyncLock() {
	var (
		count int
	)

	lock := golock.New()
	for i := 0; i < 2; i++ {
		go func() {
			for i := 1000; i > 0; i-- {
				//lock.WLock()
				//count++
				//lock.WUnlock()

				lock.WLockFunc(func(parameters ...any) {
					count++
				})
			}
			fmt.Println(count)
		}()
	}

	fmt.Scanf("\n") //等待子线程全部结束
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoLock

type GoLock struct {
	Mutex  sync.Mutex   // 读锁时不能写,写锁时不能读取
	MuteRW sync.RWMutex //读写互斥锁,并发读取,单一写入。读多写少性能会好
}

RWMutex 也称为读写互斥锁,读写互斥锁就是读取/写入互相排斥的锁。它可以由任意数量的读取操作的 goroutine 或单个写入操作的 goroutine 持有。 读写互斥锁 RWMutex 类型有五个方法,Lock,Unlock,Rlock,RUnlock 和 RLocker。其中,RLocker 返回一个 Locker 接口, 该接口通过调用 rw.RLock 和 rw.RUnlock 来实现 Lock 和 Unlock 方法。 不能拷贝锁

func New

func New() *GoLock

func (*GoLock) Lock

func (g *GoLock) Lock()

获取同步锁

func (*GoLock) LockFunc added in v1.2.5

func (g *GoLock) LockFunc(fn func(parameters ...any), parameters ...any)

共享内存加锁,同步锁

func (*GoLock) RLock added in v1.2.5

func (g *GoLock) RLock()

获取读锁

func (*GoLock) RLockFunc added in v1.2.5

func (g *GoLock) RLockFunc(fn func(parameters ...any), parameters ...any)

加读锁

func (*GoLock) RUnlock added in v1.2.5

func (g *GoLock) RUnlock()

释放读锁

func (*GoLock) Unlock added in v1.2.5

func (g *GoLock) Unlock()

释放同步锁

func (*GoLock) WLock added in v1.2.5

func (g *GoLock) WLock()

获取写锁

func (*GoLock) WLockFunc added in v1.2.5

func (g *GoLock) WLockFunc(fn func(parameters ...any), parameters ...any)

加写锁

func (*GoLock) WUnlock added in v1.2.5

func (g *GoLock) WUnlock()

释放取写锁

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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