修复garray排序数组Add变参时的死锁问题

This commit is contained in:
john
2018-09-30 15:44:48 +08:00
parent 94651d1f5c
commit bd0df135c8
4 changed files with 17 additions and 10 deletions

View File

@ -57,9 +57,9 @@ func (a *SortedIntArray) Add(values...int) {
return
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
a.mu.Unlock()
return
}
// 加到指定索引后面
@ -69,6 +69,7 @@ func (a *SortedIntArray) Add(values...int) {
rear := append([]int{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}

View File

@ -41,9 +41,9 @@ func (a *SortedArray) Add(values...interface{}) {
return
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
a.mu.Unlock()
return
}
// 加到指定索引后面
@ -53,6 +53,7 @@ func (a *SortedArray) Add(values...interface{}) {
rear := append([]interface{}{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}

View File

@ -24,7 +24,7 @@ type SortedStringArray struct {
func NewSortedStringArray(size int, cap int, safe...bool) *SortedStringArray {
a := &SortedStringArray {
mu : rwmutex.New(safe...),
mu : rwmutex.New(safe...),
unique : gtype.NewBool(),
compareFunc : func(v1, v2 string) int {
return strings.Compare(v1, v2)
@ -49,9 +49,9 @@ func (a *SortedStringArray) Add(values...string) {
return
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
a.mu.Unlock()
return
}
// 加到指定索引后面
@ -61,6 +61,7 @@ func (a *SortedStringArray) Add(values...string) {
rear := append([]string{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}
}

View File

@ -1,11 +1,15 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g/util/gregex"
)
import "gitee.com/johng/gf/g/container/garray"
type S struct {
func main() {
fmt.Println(gregex.IsMatchString("g/os/glog/glog.+$", "g/os/glog/glog_logger.go"))
}
func main() {
var source = []string{"59705a2c1fd50736a4c768a1", "597a95ff1fd5073e48bb2272", "597a960f1fd5073e48bb2274"}
var CacheChannelKeys *garray.SortedStringArray
CacheChannelKeys = garray.NewSortedStringArray(9999, 9999)
CacheChannelKeys.Add(source...)
}