Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sync"
- "time"
- sem "github.com/oscuro1111/golang-libs/semaphore"
- )
- var (
- log = fmt.Println
- )
- type Res struct {
- data int
- }
- func work(pool *sem.Semaphore, wg *sync.WaitGroup) {
- resource := pool.GetResource().(Res)
- for i := 10; i > 0; i-- {
- resource.data++
- time.Sleep(10 * time.Millisecond)
- }
- pool.ReleaseResource(resource)
- wg.Done()
- return
- }
- func main() {
- resources := []interface {
- }{
- Res{data: 0}, Res{data: 0}, Res{data: 0},
- }
- wg := &sync.WaitGroup{}
- pool := sem.NewSemaphore(resources)
- numThreads := 100
- for i := numThreads; i > 0; i-- {
- wg.Add(1)
- go work(pool, wg)
- }
- wg.Wait()
- for _, val := range resources {
- log(val.(Res))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement