并发安全容器的并发安全方法改进

This commit is contained in:
john
2018-09-06 14:48:55 +08:00
parent ebc10f7779
commit 9d82bb3f2d
19 changed files with 186 additions and 112 deletions

View File

@ -47,12 +47,21 @@ func (a *IntArray) Set(index int, value int) {
}
// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界
func (a *IntArray) Insert(index int, value int) {
func (a *IntArray) InsertBefore(index int, value int) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]int{}, a.array[index:]...)
rear := append([]int{}, a.array[index:]...)
a.array = append(a.array[0:index], value)
a.array = append(a.array, rear...)
}
// 在当前索引位置后插入一个数据项, 调用方注意判断数组边界
func (a *IntArray) InsertAfter(index int, value int) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]int{}, a.array[index+1:]...)
a.array = append(a.array[0:index+1], value)
a.array = append(a.array, rear...)
}
@ -117,14 +126,14 @@ func (a *IntArray) Search(value int) int {
// 使用自定义方法执行加锁修改操作
func (a *IntArray) LockFunc(f func(array []int)) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *IntArray) RLockFunc(f func(array []int)) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -47,7 +47,7 @@ func (a *Array) Set(index int, value interface{}) {
}
// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界
func (a *Array) Insert(index int, value interface{}) {
func (a *Array) InsertBefore(index int, value interface{}) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]interface{}{}, a.array[index : ]...)
@ -55,6 +55,15 @@ func (a *Array) Insert(index int, value interface{}) {
a.array = append(a.array, rear...)
}
// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界
func (a *Array) InsertAfter(index int, value interface{}) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]interface{}{}, a.array[index+1 : ]...)
a.array = append(a.array[0 : index+1], value)
a.array = append(a.array, rear...)
}
// 删除指定索引的数据项, 调用方注意判断数组边界
func (a *Array) Remove(index int) {
a.mu.Lock()
@ -116,14 +125,14 @@ func (a *Array) Search(value interface{}) int {
// 使用自定义方法执行加锁修改操作
func (a *Array) LockFunc(f func(array []interface{})) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *Array) RLockFunc(f func(array []interface{})) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -21,6 +21,7 @@ type SortedIntArray struct {
compareFunc func(v1, v2 int) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
}
// 创建一个排序的int数组
func NewSortedIntArray(size int, cap int, safe...bool) *SortedIntArray {
a := &SortedIntArray {
mu : rwmutex.New(safe...),
@ -168,14 +169,14 @@ func (a *SortedIntArray) Clear() {
// 使用自定义方法执行加锁修改操作
func (a *SortedIntArray) LockFunc(f func(array []int)) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *SortedIntArray) RLockFunc(f func(array []int)) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -153,14 +153,14 @@ func (a *SortedArray) Clear() {
// 使用自定义方法执行加锁修改操作
func (a *SortedArray) LockFunc(f func(array []interface{})) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *SortedArray) RLockFunc(f func(array []interface{})) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -162,14 +162,14 @@ func (a *SortedStringArray) Clear() {
// 使用自定义方法执行加锁修改操作
func (a *SortedStringArray) LockFunc(f func(array []string)) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *SortedStringArray) RLockFunc(f func(array []string)) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -48,7 +48,7 @@ func (a *StringArray) Set(index int, value string) {
}
// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界
func (a *StringArray) Insert(index int, value string) {
func (a *StringArray) InsertBefore(index int, value string) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]string{}, a.array[index:]...)
@ -56,6 +56,15 @@ func (a *StringArray) Insert(index int, value string) {
a.array = append(a.array, rear...)
}
// 在当前索引位置后插入一个数据项, 调用方注意判断数组边界
func (a *StringArray) InsertAfter(index int, value string) {
a.mu.Lock()
defer a.mu.Unlock()
rear := append([]string{}, a.array[index+1:]...)
a.array = append(a.array[0:index+1], value)
a.array = append(a.array, rear...)
}
// 删除指定索引的数据项, 调用方注意判断数组边界
func (a *StringArray) Remove(index int) {
a.mu.Lock()
@ -116,14 +125,14 @@ func (a *StringArray) Search(value string) int {
// 使用自定义方法执行加锁修改操作
func (a *StringArray) LockFunc(f func(array []string)) {
a.mu.Lock()
defer a.mu.Unlock()
a.mu.Lock(true)
defer a.mu.Unlock(true)
f(a.array)
}
// 使用自定义方法执行加锁读取操作
func (a *StringArray) RLockFunc(f func(array []string)) {
a.mu.RLock()
defer a.mu.RUnlock()
a.mu.RLock(true)
defer a.mu.RUnlock(true)
f(a.array)
}

View File

@ -161,16 +161,16 @@ func (this *IntBoolMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *IntBoolMap) LockFunc(f func(m map[int]bool)) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *IntBoolMap) RLockFunc(f func(m map[int]bool)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -161,16 +161,16 @@ func (this *IntIntMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *IntIntMap) LockFunc(f func(m map[int]int)) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *IntIntMap) RLockFunc(f func(m map[int]int)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -159,16 +159,16 @@ func (this *IntInterfaceMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *IntInterfaceMap) LockFunc(f func(m map[int]interface{})) {
this.mu.Lock()
defer this.mu.Unlock()
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *IntInterfaceMap) RLockFunc(f func(m map[int]interface{})) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -161,16 +161,16 @@ func (this *IntStringMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *IntStringMap) LockFunc(f func(m map[int]string)) {
this.mu.Lock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
this.mu.Unlock()
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *IntStringMap) RLockFunc(f func(m map[int]string)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -161,16 +161,16 @@ func (this *InterfaceInterfaceMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *InterfaceInterfaceMap) LockFunc(f func(m map[interface{}]interface{})) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *InterfaceInterfaceMap) RLockFunc(f func(m map[interface{}]interface{})) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -161,16 +161,16 @@ func (this *StringBoolMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全锁操作,使用自定义方法执行加锁修改操作
func (this *StringBoolMap) LockFunc(f func(m map[string]bool)) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全锁操作,使用自定义方法执行加锁读取操作
func (this *StringBoolMap) RLockFunc(f func(m map[string]bool)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -159,16 +159,16 @@ func (this *StringIntMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
func (this *StringIntMap) LockFunc(f func(m map[string]int)) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
func (this *StringIntMap) RLockFunc(f func(m map[string]int)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -161,16 +161,16 @@ func (this *StringInterfaceMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
func (this *StringInterfaceMap) LockFunc(f func(m map[string]interface{})) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
func (this *StringInterfaceMap) RLockFunc(f func(m map[string]interface{})) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -159,16 +159,16 @@ func (this *StringStringMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
func (this *StringStringMap) LockFunc(f func(m map[string]string)) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
func (this *StringStringMap) RLockFunc(f func(m map[string]string)) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -160,16 +160,16 @@ func (this *UintInterfaceMap) Clear() {
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
func (this *UintInterfaceMap) LockFunc(f func(m map[uint]interface{})) {
this.mu.Lock()
defer this.mu.Unlock()
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
// 使用自定义方法执行加锁读取操作
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
func (this *UintInterfaceMap) RLockFunc(f func(m map[uint]interface{})) {
this.mu.RLock()
defer this.mu.RUnlock()
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
}

View File

@ -19,26 +19,26 @@ func New(safe...bool) *RWMutex {
return mu
}
func (mu *RWMutex) Lock() {
if mu.safe {
func (mu *RWMutex) Lock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.Lock()
}
}
func (mu *RWMutex) Unlock() {
if mu.safe {
func (mu *RWMutex) Unlock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.Unlock()
}
}
func (mu *RWMutex) RLock() {
if mu.safe {
func (mu *RWMutex) RLock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.RLock()
}
}
func (mu *RWMutex) RUnlock() {
if mu.safe {
func (mu *RWMutex) RUnlock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.RUnlock()
}
}

View File

@ -7,21 +7,53 @@ import (
func main () {
a := garray.NewSortedArray(0, 0, func(v1, v2 interface{}) int {
if v1.(int) < v2.(int) {
return -1
}
if v1.(int) > v2.(int) {
return 1
}
return 0
// 创建普通的int类型数组并关闭默认的并发安全特性
a := garray.NewIntArray(0, 0, false)
// 添加数据项
for i := 0; i < 10; i++ {
a.Append(i)
}
// 获取当前数组长度
fmt.Println(a.Len())
// 获取当前数据项列表
fmt.Println(a.Slice())
// 获取指定索引项
fmt.Println(a.Get(6))
// 在指定索引前插入数据项
a.InsertAfter(9, 11)
// 在指定索引后插入数据项
a.InsertBefore(10, 10)
fmt.Println(a.Slice())
// 修改指定索引的数据项
a.Set(0, 100)
fmt.Println(a.Slice())
// 搜索数据项,返回搜索到的索引位置
fmt.Println(a.Search(5))
// 删除指定索引的数据项
a.Remove(0)
fmt.Println(a.Slice())
// 并发安全,写锁操作
a.LockFunc(func(array []int) {
// 将末尾项改为100
array[len(array) - 1] = 100
})
a.Add(10)
a.Add(20)
a.Add(30)
a.Add(1)
a.Add(1)
a.Add(1)
a.Add(1)
// 并发安全,读锁操作
a.RLockFunc(func(array []int) {
fmt.Println(array[len(array) - 1])
})
// 清空数组
fmt.Println(a.Slice())
a.Clear()
fmt.Println(a.Slice())
}

View File

@ -7,8 +7,22 @@ import (
func main () {
a := garray.NewSortedIntArray(0)
a.Add(0)
// 自定义排序数组,降序排序
a := garray.NewSortedArray(0, 0, func(v1, v2 interface{}) int {
if v1.(int) < v2.(int) {
return 1
}
if v1.(int) > v2.(int) {
return -1
}
return 0
})
a.Add(10)
a.Add(20)
a.Add(30)
a.Add(1)
a.Add(1)
a.Add(1)
a.Add(1)
fmt.Println(a.Slice())
fmt.Println(a.Search(0))
}