35 lines
482 B
Go
35 lines
482 B
Go
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]{}
|
|
}
|