Last active
December 28, 2016 21:10
-
-
Save skateman/c9e1f32c8e96c9d317aa20ecbc3b90b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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