package main
import (
"sync"
"github.com/xiaotushaoxia/multilock"
)
func main() {
var ml = multilock.NewVariable[int]()
mp := map[int]map[int]int{}
count := 1000
for i := 0; i < count; i++ {
mp[i%10] = map[int]int{}
}
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
seg := i % 10
ii := i
if ii%4 == 0 {
go func() {
defer wg.Done()
unlock := ml.Lock(seg)
defer unlock()
mp[seg][ii] = ii
}()
} else if ii%4 == 1 {
go func() {
defer wg.Done()
ml.LockByKey(seg)
defer ml.UnlockByKey(seg)
mp[seg][ii] = ii
}()
} else if ii%4 == 3 {
go func() {
defer wg.Done()
if got, unlock := ml.TryLock(seg); got {
defer unlock()
mp[seg][ii] = ii
}
}()
} else {
go func() {
defer wg.Done()
if ml.TryLockByKey(seg) {
mp[seg][ii] = ii
ml.UnlockByKey(seg)
}
}()
}
}
wg.Wait()
}
-
Notifications
You must be signed in to change notification settings - Fork 0
xiaotushaoxia/multilock
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Packages 0
No packages published