新增异步队列

This commit is contained in:
boyce
2019-03-26 15:03:05 +08:00
parent b0156ce788
commit de87bd847d
7 changed files with 400 additions and 0 deletions

51
util/queue/syncqueue.go Normal file
View File

@@ -0,0 +1,51 @@
package queue
import "sync"
type SyncQueue struct {
que *Queue
mutex sync.RWMutex
}
func (q *SyncQueue) Len() int {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Length()
}
func (q *SyncQueue) Add(elem interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
q.que.Add(elem)
}
func (q *SyncQueue) Peek() interface{} {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Peek()
}
func (q *SyncQueue) Get(i int) interface{} {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Get(i)
}
func (q *SyncQueue) Pop() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
return q.que.Pop()
}
func NewSyncQueue() *SyncQueue {
syncQueue := SyncQueue{}
syncQueue.que = NewQueue()
return &syncQueue
}