Skip to content

Instantly share code, notes, and snippets.

@skateman
Last active December 28, 2016 21:10
Show Gist options
  • Save skateman/c9e1f32c8e96c9d317aa20ecbc3b90b3 to your computer and use it in GitHub Desktop.
Save skateman/c9e1f32c8e96c9d317aa20ecbc3b90b3 to your computer and use it in GitHub Desktop.
package semaphore
import "sync"
type Semaphore struct {
Channel chan uint
Mutex sync.Mutex
}
func New(size uint) *Semaphore {
return &Semaphore{
Channel: make(chan uint, size),
Mutex: sync.Mutex{},
}
}
func (s *Semaphore) Lock(size uint) {
s.Mutex.Lock()
for uint(i):=0; i<size; i++ {
s.Channel <- i
}
s.Mutex.Unlock()
}
func (s *Semaphore) Unlock(size uint) {
for uint(i):=0; i<size; i++ {
<- s.Channel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment