33 lines
487 B
Go
33 lines
487 B
Go
|
package containers
|
||
|
|
||
|
import "sync"
|
||
|
|
||
|
type LockableStack[T any] struct {
|
||
|
sync.Mutex
|
||
|
data []T
|
||
|
}
|
||
|
|
||
|
func (s *LockableStack[T]) Push(t T) {
|
||
|
s.Lock()
|
||
|
defer s.Unlock()
|
||
|
|
||
|
s.data = append(s.data, t)
|
||
|
}
|
||
|
|
||
|
func (s *LockableStack[T]) Pop() (T, bool) {
|
||
|
s.Lock()
|
||
|
defer s.Unlock()
|
||
|
|
||
|
if len(s.data) == 0 {
|
||
|
var t T
|
||
|
return t, false
|
||
|
}
|
||
|
res := s.data[len(s.data)-1]
|
||
|
s.data = s.data[:len(s.data)-1]
|
||
|
return res, true
|
||
|
}
|
||
|
|
||
|
func NewLockableStack[T any]() *LockableStack[T] {
|
||
|
return &LockableStack[T]{}
|
||
|
}
|