containers/lockablequeue.go

35 lines
482 B
Go
Raw Permalink Normal View History

2022-07-17 03:04:04 -04:00
package containers
import "sync"
type LockableQueue[T any] struct {
sync.Mutex
items []T
}
func (q *LockableQueue[T]) Push(item T) {
q.Lock()
defer q.Unlock()
q.items = append(q.items, item)
}
func (q *LockableQueue[T]) Pop() (T, bool) {
q.Lock()
defer q.Unlock()
if len(q.items) == 0 {
var res T
return res, false
}
res := q.items[0]
q.items = q.items[1:]
return res, true
}
func NewLockableQueue[T any]() *LockableQueue[T] {
return &LockableQueue[T]{}
}