repl.it
@dorin131/

counting semaphore

Go

No description

fork
loading
Files
  • main.go
  • Packager files
  • go.mod
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
  "fmt"
  "sync"
  "time"
)

var tokens = make(chan struct{}, 3)

func main() {
  wg := sync.WaitGroup{}
  defer wg.Wait()
  for n := range []int{0,1,2,3,4,5,6,7,8,9} {
    wg.Add(1)
    go doInParallel(&wg, n)
  }
}

func doInParallel(wg* sync.WaitGroup, n int) {
  tokens <- struct{}{}
  fmt.Println(n)
  time.Sleep(1000 * time.Millisecond)
  <- tokens
  defer wg.Done()
}
Fetching token
?