Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package semaphore
- type Semaphore struct {
- ch chan struct{}
- }
- // Create semaphore
- func New(size int) *Semaphore {
- if size < 1 {
- size = 1
- }
- return &Semaphore{ch: make(chan struct{}, size)}
- }
- func (s *Semaphore) Acquire() {
- s.ch <- struct{}{}
- }
- func (s *Semaphore) Release() {
- <-s.ch
- }
- func (s *Semaphore) Size() int {
- return cap(s.ch)
- }
- func (s *Semaphore) Free() int {
- return cap(s.ch) - len(s.ch)
- }
Add Comment
Please, Sign In to add comment