queue add RLockRange method.

This commit is contained in:
boyce
2019-03-26 16:11:39 +08:00
parent 83f38bcf22
commit a854355952
2 changed files with 10 additions and 1 deletions

View File

@@ -78,7 +78,7 @@ func (q *Queue) Get(i int) interface{} {
i += q.count i += q.count
} }
if i < 0 || i >= q.count { if i < 0 || i >= q.count {
panic("queue: Get() called with index out of range") return nil
} }
// bitwise modulus // bitwise modulus
return q.buf[(q.head+i)&(len(q.buf)-1)] return q.buf[(q.head+i)&(len(q.buf)-1)]

View File

@@ -43,6 +43,15 @@ func (q *SyncQueue) Pop() interface{} {
return q.que.Pop() return q.que.Pop()
} }
func (q *SyncQueue) RLockRange(f func(interface{})) {
q.mutex.RLock()
defer q.mutex.RUnlock()
for i := 0; i < q.que.Length(); i++ {
f(q.Get(i))
}
}
func NewSyncQueue() *SyncQueue { func NewSyncQueue() *SyncQueue {
syncQueue := SyncQueue{} syncQueue := SyncQueue{}
syncQueue.que = NewQueue() syncQueue.que = NewQueue()