Giter VIP home page Giter VIP logo

Comments (3)

WeiJiadong avatar WeiJiadong commented on June 21, 2024

是需要类似这样的实现吗?其实就是预申请很多把锁,对加解锁进行封装,以及通过hash的方式来取用锁。

package main

import (
	"fmt"
	"hash/fnv"
	"sync"
	"time"
)

type SegmentKeysLock struct {
	locks []sync.RWMutex
}

func NewSegmentKeysLock(size int) *SegmentKeysLock {
	return &SegmentKeysLock{
		locks: make([]sync.RWMutex, size),
	}
}

func (s *SegmentKeysLock) hash(key string) uint32 {
	h := fnv.New32a()
	h.Write([]byte(key))
	return h.Sum32()
}

func (s *SegmentKeysLock) RLock(key string) {
	hash := s.hash(key)
	lock := &s.locks[hash%uint32(len(s.locks))]
	lock.RLock()
}

func (s *SegmentKeysLock) RUnlock(key string) {
	hash := s.hash(key)
	lock := &s.locks[hash%uint32(len(s.locks))]
	lock.RUnlock()
}

func (s *SegmentKeysLock) Lock(key string) {
	hash := s.hash(key)
	lock := &s.locks[hash%uint32(len(s.locks))]
	lock.Lock()
}

func (s *SegmentKeysLock) Unlock(key string) {
	hash := s.hash(key)
	lock := &s.locks[hash%uint32(len(s.locks))]
	lock.Unlock()
}

func main() {
	lock := NewSegmentKeysLock(10)

	go func() {
		lock.Lock("key1")
		fmt.Println("goroutine 1 locked key1")
		time.Sleep(2 * time.Second)
		lock.Unlock("key1")
		fmt.Println("goroutine 1 unlocked key1")
	}()

	go func() {
		time.Sleep(1 * time.Second)
		lock.RLock("key1")
		fmt.Println("goroutine 2 read-locked key1")
		lock.RUnlock("key1")
		fmt.Println("goroutine 2 read-unlocked key1")
	}()

	time.Sleep(3 * time.Second)
}

from ekit.

flycash avatar flycash commented on June 21, 2024

是的

from ekit.

WeiJiadong avatar WeiJiadong commented on June 21, 2024

好,已提交pr:
#225

from ekit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.