https://github.com/cydave/semaphore
A semaphore
https://github.com/cydave/semaphore
Last synced: about 2 months ago
JSON representation
A semaphore
- Host: GitHub
- URL: https://github.com/cydave/semaphore
- Owner: cydave
- License: gpl-3.0
- Created: 2023-10-19T08:59:40.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-19T09:13:01.000Z (over 2 years ago)
- Last Synced: 2025-01-13T07:27:51.599Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Semaphore
A simple semaphore.
## Example
```go
package main
import (
"fmt"
"math/rand"
"time"
"github.com/cydave/semaphore"
)
func doWork(workerNum int, sem semaphore.Semaphore) {
defer sem.Release()
fmt.Printf("Worker %d processing\n", workerNum)
n := rand.Intn(500)
time.Sleep(time.Duration(n * int(time.Millisecond)))
time.Sleep(time.Duration(n * int(time.Millisecond)))
fmt.Printf("Worker %d finished\n", workerNum)
}
func main() {
nWorkers := 10
sem := semaphore.New(nWorkers)
defer sem.Close()
for i := 0; i < 100; i++ {
sem.Acquire()
go doWork((i%nWorkers)+1, sem)
}
fmt.Println("Done!")
}
```